首页 > 解决方案 > 来自桶加密的 boto3 打印规则

问题描述

我正在编写一个 python 脚本来检索 AWS 的信息并且我试图只获取 SSEAlgorith 但我得到TypeError: list indices must be integers or slices, not str 有没有办法做到这一点?我想这是为了 [] 里面的规则。

{
    "ServerSideEncryptionConfiguration": {
        "Rules": [
            {
                "ApplyServerSideEncryptionByDefault": {
                    "SSEAlgorithm": "AES256"
                }
            }
        ]
    }
}

这是我用来检索信息的代码:

s3 = boto3.client('s3') 
buc = s3.list_buckets()

for i in response['Buckets']:
    enc = s3.get_bucket_encryption(Bucket=i['Name'])
    rules = enc['ServerSideEncryptionConfiguration']['Rules']['ApplyServerSideEncryptionByDefault']['SSEAlgorithm']
    print(rules)

标签: python-3.xamazon-s3boto3botobotocore

解决方案


Rules是一个列表。所以假设你只有一个列表,它应该是:

rules = enc['ServerSideEncryptionConfiguration']['Rules'][0]['ApplyServerSideEncryptionByDefault']['SSEAlgorithm']

推荐阅读