我尝试了以下方法来获取s3.Bucket.objectsCollectionboto3 v1.7.37 中的 len/content_length :
import boto3
s3 = boto3.resource('s3')
bucket = s3.Bucket('myBucket')
bucketObjects = bucket.objects.filter(Prefix='myPrefix')
if (len(bucketObjects) > 0):
do_something()
else:
do_something_else()
Run Code Online (Sandbox Code Playgroud)
不幸的是,这给了我以下错误:
TypeError: object of type 's3.Bucket.objectsCollection' has no len()
Run Code Online (Sandbox Code Playgroud)
我也试过这个bucketobjects.content_length并得到
AttributeError: 's3.Bucket.objectsCollection' object has no attribute 'content_length'
Run Code Online (Sandbox Code Playgroud)
我是否必须遍历列表并计算对象或有更好的方法?