根据有关包含句点(example)的存储桶名称问题的评论,该区域似乎很重要,因此我想从一开始就通过从已知存储桶中抓取region_name
来解决该问题:< / p>
import boto3
s3 = boto3.resource('s3')
region = s3.meta.client.get_bucket_location(Bucket='some.topLevel.BucketPath.withPeriods')['LocationConstraint']
#set region in resource
s3 = boto3.resource('s3',region_name=region)
要查看顶级的所有存储桶:
for bucket in s3.buckets.all():
print(bucket.name)
如果我们想查看'some.topLevel.BucketPath.withPeriods'的内容:
x = s3.Bucket('some.topLevel.BucketPath.withPeriods')
for item in x.objects.all():
print item
以下内容还应提供信息:
s3.meta.client.head_bucket(Bucket='some.topLevel.BucketPath.withPeriods')
s3.meta.client.get_bucket_acl(Bucket='some.topLevel.BucketPath.withPeriods')
将一些测试文件上传到适当的存储桶:
with open ('~/someFile.pdf', 'rb') as data:
s3.meta.client.put_object(Bucket='some.topLevel.BucketPath.withPeriods',Key='s3SubDir1/s3SubDir2/someTestFile1.pdf', Body=data)
with open ('~/someFile.pdf', 'rb') as data:
s3.Bucket('some.topLevel.BucketPath.withPeriods').put_object(Key='s3SubDir1/s3SubDir2/someTestFile2.pdf', Body=data)
问题:
1.我们如何查看路径some.topLevel.BucketPath.withPeriods/s3SubDir1/s3SubDir2/
的内容?
我开始走这条路:
x = s3.Bucket('some.topLevel.BucketPath.withPeriods')
for item in x.objects.all():
for thing in item.objects.all():
print thing
ERROR:
AttributeError Traceback (most recent call last)
<ipython-input-38-bc4079ea1f49> in <module>()
1 x = s3.Bucket('some.topLevel.BucketPath.withPeriods')
2 for item in x.objects.all():
----> 3 for thing in item.objects.all():
4 print thing
AttributeError: 's3.ObjectSummary' object has no attribute 'objects'
非常感谢任何帮助。
答案 0 :(得分:0)
您可以使用以下代码在一个存储桶中获取文件列表:
file_list = []
for file in my_bucket.objects.all():
# you can also get what type file you want
if file.key.endswith('.docx'):
file_list.append(file.key)
返回结果file_list是此S3中包含的所有文件。