获得存储桶大小所需的S3权限? [英] S3 permissions required to get bucket size?

查看:77
本文介绍了获得存储桶大小所需的S3权限?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用boto3来获取S3中所有对象的大小,并授予了以下权限:

  s3:ListAllMyBucketss3:ListObjectss3:GetObject 

但是boto不断抛出此错误:

 在调用ListObjects操作时发生错误(AccessDenied):访问被拒绝 

我在文档中或通过查看boto的源代码都找不到任何详细信息...没有人知道仅仅为了获得S3存储桶中所有对象的大小所需的最低权限吗?

解决方案

我创建了以下lambda,该lambda打印每个对象的大小并汇总了存储桶的总大小.

我从

如果尝试上述解决方案后仍然发现问题,请查看此 解决方案

I created the following lambda which prints each object size and sums up the total bucket size.

I use convert_size function from here. Credit to @James Sapam.

Code snippet :

import boto3
import math

def convert_size(size_bytes):
   if size_bytes == 0:
       return "0B"
   size_name = ("B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB")
   i = int(math.floor(math.log(size_bytes, 1024)))
   p = math.pow(1024, i)
   s = round(size_bytes / p, 2)
   return "%s %s" % (s, size_name[i])

bucket_name = 'BUCKET_NAME'
s3 = boto3.resource('s3')
bucket = s3.Bucket(bucket_name)

def lambda_handler(event, context):
    all_objects = bucket.objects.all()
    total_size = 0
    for obj in all_objects:
        if obj.key.split('/')[-1]:
            file_name = obj.key
            file_size = convert_size(obj.size)
            total_size += obj.size
            print("File Name: %s File Size: %s" % (file_name,file_size) ) 
    print("%s bucket size : %s" % (bucket_name,convert_size(total_size)) )

Policy summary JSON :

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "VisualEditor0",
            "Effect": "Allow",
            "Action": [
                "s3:GetObject",
                "s3:ListBucket"
            ],
            "Resource": "arn:aws:s3:::${BUCKET_NAME}"
        }
    ]
}

Output :

If after trying the above solution you still find issues, take a look at this thread.

这篇关于获得存储桶大小所需的S3权限?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆