如何获得boto3集合的大小? [英] How do I get the size of a boto3 Collection?
问题描述
我一直使用的方式是将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?
推荐答案
这些对象是AWS S3的限制(请参见 https://forums.aws.amazon.com/thread .jspa?messageID = 164220 )。
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).
获取对象摘要(HEAD)没有获得实际数据,所以应该是一个相对便宜的操作,如果你只是舍弃该列表,然后你可以这样做:
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.
这篇关于如何获得boto3集合的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!