You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

boto3获取AWS S3桶加密SSEAlgorithm报TypeError问题求解

错误根因

你遇到的TypeError: list indices must be integers or slices, not str确实是Rules字段为数组类型导致的。从返回的配置结构可以看到,Rules是方括号包裹的列表,存储加密规则集合,无法直接用字符串键索引列表,必须先指定数组下标取值。
另外你当前代码还存在变量名不匹配问题:调用list_buckets()的结果赋值给了buc变量,但循环遍历桶时用了未定义的response变量,直接运行会额外抛出NameError。

修正后可运行代码
import boto3
from botocore.exceptions import ClientError

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

for i in buc['Buckets']:
    bucket_name = i['Name']
    try:
        enc = s3.get_bucket_encryption(Bucket=bucket_name)
        # Rules是数组,先取下标0取第一条规则(AWS S3目前仅支持1条加密规则)
        sse_algo = enc['ServerSideEncryptionConfiguration']['Rules'][0]['ApplyServerSideEncryptionByDefault']['SSEAlgorithm']
        print(f"桶{bucket_name}的加密算法为:{sse_algo}")
    except ClientError as e:
        # 未配置加密的桶会抛出ClientError,此处做兼容处理
        if e.response['Error']['Code'] == 'ServerSideEncryptionConfigurationNotFoundError':
            print(f"桶{bucket_name}未配置默认加密")
        else:
            print(f"获取桶{bucket_name}加密配置失败:{e}")
注意事项
  • AWS S3的默认加密规则目前仅支持配置1条,所以固定取Rules[0]即可覆盖所有合规配置场景
  • 建议始终增加异常捕获逻辑,避免未开启加密的桶导致程序中断运行

内容的提问来源于stack exchange,提问作者Jean Lugo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.02 10:57:03