Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the size of a boto3 Collection?

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 = len(list(bucket.objects.all())) 

However, this forces resolution of the whole collection and obviates the benefits of using a Collection in the first place. Is there a better way to do this?

like image 346
Rahul Gupta-Iwasaki Avatar asked Sep 05 '15 00:09

Rahul Gupta-Iwasaki


People also ask

What is boto3 client (' S3 ')?

​Boto3 is the official AWS SDK for Python, used to create, configure, and manage AWS services. The following are examples of defining a resource/client in boto3 for the Weka S3 service, managing credentials, and pre-signed URLs, generating secure temporary tokens, and using those to run S3 API calls.

What is the difference between boto3 client and resource?

00:00 Boto3's primary function is to make AWS API calls for you. It extracts these APIs in two main ways: clients and resources. Clients give you low-level service access, while resources provide an object-oriented way of working with these services.


1 Answers

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.

like image 66
AChampion Avatar answered Oct 23 '22 10:10

AChampion