How do I get the size of a boto3 Collection?

前端 未结 3 1310
攒了一身酷
攒了一身酷 2021-02-07 00:34

The way I have been using is to transform the Collection into a List and query the length:

s3 = boto3.resource(\'s3\')
bucket = s3.Bucket(\'my_bucket\')
size = l         


        
3条回答
  •  佛祖请我去吃肉
    2021-02-07 01:11

    There is no way to get the count of keys in a bucket without listing all the objects this is a limitation of AWS S3 (see https://forums.aws.amazon.com/thread.jspa?messageID=164220).

    Getting the Object Summaries (HEAD) doesn't get the actual data so should be a relatively inexpensive operation and if you are just discarding the list then you could do:

    size = sum(1 for _ in bucket.objects.all())
    

    Which will give you the number of objects without constructing a list.

提交回复
热议问题