使用Boto3无法获得桶的总大小

时间:2017-10-01 13:39:42

标签: python amazon-web-services amazon-s3 boto boto3

我正在尝试获取水桶的总大小。但是,total_size返回0.当然,存储桶中有几个文件。如果我的存储桶中有五个文件,则以下功能会打印五个零。我做错了什么?

bucket = boto3.resource('s3', config=Config(signature_version="s3", s3={'addressing_style': 'path'})).Bucket(name)
for object in bucket.objects.all():
    total_size += object.size
    print(object.size)

4 个答案:

答案 0 :(得分:5)

我看到几个问题:

  • 不确定您对public ImmutableList<T> Add(T item) { // Create a new list with the added item } IImmutableList<T> IImmutableList<T>.Add(T value) => this.Add(value); void ICollection<T>.Add(T item) => throw new NotSupportedException(); int IList.Add(object value) => throw new NotSupportedException(); 的来电。这是对的吗?
  • boto3.resource()未初始化

试试这个:

total_size

或者一个班轮:

total_size = 0
bucket = boto3.resource('s3').Bucket('mybucket')
for object in bucket.objects.all():
  total_size += object.size
  print(object.size)
print(total_size)

答案 1 :(得分:1)

将signature_version =“s3”更改为signature_version =“s3v4”。

我也喜欢helloV的答案。

同时指定存储区的区域,而不是依赖于默认配置。

答案 2 :(得分:1)

更简单的替代方法是使用Amazon S3 Inventory每天转储对象列表,然后从中计算总数。

答案 3 :(得分:0)

我正在使用这个:

s3client = boto3.client('s3', region_name=region,
                            aws_access_key_id=access_key,
                            aws_secret_access_key=secret_key)
response = s3client.list_objects(Bucket=bucket_name)['Contents']
bucket_size = sum(obj['Size'] for obj in response)