简体   繁体   English

Boto3-查看所有目录和文件的python脚本

[英]Boto3 - python script to view all directories and files

I tried to follow the Boto3 examples, but can literally only manage to get the very basic listing of all my S3 buckets via the example they give: 我尝试遵循Boto3示例,但实际上只能通过它们提供的示例来设法获得所有S3存储桶的最基本清单:

import boto3
s3 = boto3.resource('s3')
for bucket in s3.buckets.all():
    print(bucket.name)

I cannot find documentation that explains how I would be able to traverse or change into folders and then access individual files. 我找不到说明如何能够遍历或更改到文件夹然后访问单个文件的文档。

I'm trying to get to my SNS delivery reports, which are stored in a folder for each day of the month - so it is a pain to manually have to download each file for the month and then to concatenate the contents of each file in order to get the count of all SMS messages sent for a month. 我正在尝试获取我的SNS传递报告,该报告存储在每个月的每个月的文件夹中-因此,必须手动下载该月的每个文件,然后将每个文件的内容串联起来,这是一件很痛苦的事情为了获得一个月内发送的所有SMS消息的计数。

Does anyone have an example of a script that can help me with this, or pointers to really basic documentation/examples of helping me to do this? 有没有人提供一个可以帮助我解决此问题的脚本示例,或指向真正的基础文档/帮助我完成此操作的示例的指针?

I have 3 S3 buckets, and all the files are located in sub folders in one of them: 我有3个S3存储桶,所有文件都位于其中一个的子文件夹中:

bucketname
|->Year
  |->Month
     |->Day1
     |->Day2
     |->Day3 
     |->Day4

etc etc Underneath the "Day" folder is a single text file called 001.txt SO I am trying to concatenate all the 001.txt files for each day of a month and then find the rowcount of the concatenated text file - which would give me the count of all SMS sent - successful and failed. 等等等等在“ Day”文件夹下是一个名为001.txt的文本文件,所以我尝试将一个月的每一天都连接所有001.txt文件,然后找到连接的文本文件的行数-这将给我发送的所有SMS的计数-成功和失败。

Any help, much appreciated. 任何帮助,不胜感激。

There are no folders, only S3 object keys. 没有文件夹,只有S3对象键。

Using the Bucket Resource interface, you can filter the list of objects in a bucket using the objects collection filter() method (see example ). 使用存储桶资源接口,可以使用对象集合 filter()方法过滤存储桶中的对象列表(请参阅示例 )。

You can also use the Client interface to call list_objects() with a suitable prefix and delimiter to retrieve subsets of objects. 您还可以使用Client接口使用合适的前缀和分隔符来调用list_objects()来检索对象的子集。

See Listing Keys Hierarchically for a high-level description. 有关高级描述,请参见分层列出密钥

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM