2017-06-19 46 views
0

所以这里是我的代码,基本上它打印了S3存储桶的内容。S3:有没有办法将objects.all转换成Python列表?

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

你尝试'名单(bucket.objects.all())'? –

+0

是的,那是有效的。 -_- 谢谢! – feynmanium

回答

1

它应该是一个列表,你不需要再次迭代列表。这里的DOC:

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

+0

'print(type(bucket.objects.all()))''''class'boto3.resources.collection.s3.Bucket.objectsCollection'>' – feynmanium

相关问题