How do I get the size of a boto3 Collection?

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.

Leave a Comment

tech