S3:有没有办法将objects.all转换成Python列表?

时间:2017-06-19 18:54:01

标签: python python-3.x boto3

所以这是我的代码,基本上它打印了S3存储桶的内容。

s3 = boto3.resource('s3')
bucket_name = 'test-bucket-2'

bucket = s3.Bucket(bucket_name)
def print_object_list():
    for object in bucket.objects.all():
        print(object)

当我运行print_object_list()时,我得到:

s3.ObjectSummary(bucket_name='test-bucket-2', key=u'Guest.txt')
s3.ObjectSummary(bucket_name='test-bucket-2', key=u'infolder1/')
s3.ObjectSummary(bucket_name='test-bucket-2', key=u'infolder1/Guest.txt')
s3.ObjectSummary(bucket_name='test-bucket-2', key=u'infolder2/')
s3.ObjectSummary(bucket_name='test-bucket-2', key=u'infolder3/')
s3.ObjectSummary(bucket_name='test-bucket-2', key=u'infolder4/')

哪个好,但我想知道是否有办法将这些输出作为字符串存储在列表中。我试图将每个输出附加到一个空列表,并在最后得到一个空列表。字符串转换也不起作用。

1 个答案:

答案 0 :(得分:1)

它应该是一个列表,您不需要再次迭代列表。这是文档:

A collection of ObjectSummary resources

all()
Creates an iterable of all ObjectSummary resources in the collection.

See also: AWS API Documentation

Request Syntax

object_summary_iterator = bucket.objects.all()
Return type
list(s3.ObjectSummary)
Returns
A list of ObjectSummary resources

了解详情from the doc