【问题标题】:boto3 type error when enabling s3 bucket encryption启用 s3 存储桶加密时出现 boto3 类型错误
【发布时间】:2020-09-14 05:57:34
【问题描述】:

我目前在尝试启用服务器端存储桶加密时收到此错误:

{
  "errorMessage": "expected string or bytes-like object",
  "errorType": "TypeError",
  "stackTrace": [
    [
      "/var/task/s3EncryptionCompliance.py",
      8,
      "handler",
      "response = client.put_bucket_encryption(Bucket= bucket, ContentMD5='string', ServerSideEncryptionConfiguration={'Rules': [{'ApplyServerSideEncryptionByDefault': {'SSEAlgorithm': 'AES256'}},]})"
    ],
    [

我正在尝试遍历存储桶列表并向列表中的所有存储桶添加加密。这是我的代码: 导入 boto3 客户端 = boto3.client('s3') s3 = boto3.resource('s3')

def handler(event, context):
    response = client.list_buckets()
    for bucket in response['Buckets']:
        response = client.put_bucket_encryption(Bucket= bucket, ContentMD5='string', ServerSideEncryptionConfiguration={'Rules': [{'ApplyServerSideEncryptionByDefault': {'SSEAlgorithm': 'AES256'}},]})

谢谢你!!

【问题讨论】:

    标签: boto3


    【解决方案1】:

    list_buckets 返回 Bucket 字典 的列表。因此,要在您的情况下获得名称,您必须使用 bucket["Name"]

    因此,您的功能可能是(假设其他一切都正确):

    def handler(event, context):
        response = client.list_buckets()
        for bucket in response['Buckets']:
            response = client.put_bucket_encryption(Bucket=bucket["Name"], ContentMD5='string', ServerSideEncryptionConfiguration={'Rules': [{'ApplyServerSideEncryptionByDefault': {'SSEAlgorithm': 'AES256'}},]})
    

    【讨论】:

    • 这很好用,谢谢。现在我需要了解作为 ContentMD5 传递的内容
    • 接受!你知道 contentMD5 要求什么吗?
    • @aroe 当您要设置 SSE-C 加密时使用。您提供密钥的 MD5 哈希,以确保它在提交到 S3 时不会损坏。
    • 嗯,好的,我无法访问密钥,我不确定要传递什么或如何获取密钥
    • @aroe ContentMD5 不是必需的。你只是没有在你的情况下提供它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-08-28
    • 2014-02-05
    • 2021-11-25
    • 2015-10-28
    • 2018-06-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多