我有boto代码可将level3文件夹中的S3子文件夹收集起来:
import boto
s3 = boto.connect_s3()
bucket = s3.get_bucket("MyBucket")
for level2 in bucket.list(prefix="levelOne/", delimiter="/"):
print(level2.name)
请帮助发现boto3中的类似功能。该代码不应遍历所有S3对象,因为存储桶中有大量对象。
答案 0 :(得分:0)
我认为以下内容应等效:
import boto3
s3 = boto3.resource('s3')
bucket = s3.Bucket('MyBucket')
for object in bucket.objects.filter(Prefix="levelOne/", Delimiter="/"):
print(object.key)
答案 1 :(得分:0)
如果您只是在寻找文件夹列表,则在列出对象时使用返回的CommonPrefixes
。请注意,必须指定Delimiter
才能获得CommonPrefixes
:
import boto3
s3_client = boto3.client('s3')
response = s3_client.list_objects_v2(Bucket='BUCKET-NAME', Delimiter = '/')
for prefix in response['CommonPrefixes']:
print(prefix['Prefix'][:-1])
如果存储桶中有 HUGE 个文件夹和对象,则可以考虑使用Amazon S3 Inventory,它可以提供每日或每周的CSV文件,列出所有对象。