【问题标题】:boto3 Client Error: Server Side Encryption with Customer provided key is incompatible with the encryption method specifiedboto3 客户端错误:使用客户提供的密钥进行服务器端加密与指定的加密方法不兼容
【发布时间】:2016-10-16 10:32:27
【问题描述】:

我在我的 django 应用程序中使用 boto3 将一些文件上传到 S3。但是当我尝试使用 boto3's Object's API 指定客户端加密算法和密钥时收到以下错误。

调用 PutObject 时发生错误 (InvalidArgument) 操作:使用客户提供的密钥进行服务器端加密是 与指定的加密方法不兼容。

这是我指定加密算法和密钥的代码。

    import boto3
    s3 = boto3.resource('s3')
    key = s3.Object(bucket_name, key_name)
    file_obj.seek(0)
    kwargs = {
        'ServerSideEncryption': 'AES256',
        'SSECustomerAlgorithm': 'AES256',
        'SSECustomerKey': settings.AWS_ENCRYPTION_KEY,
    }

    key.put(**kwargs)
    key.put(Body=file_obj)
    key.Acl().put(ACL='public-read')

这是我在 settings.py 中生成加密密钥的方式

# settings.py
password = '32characterslongpassphraseneeded'.encode('utf-8')
AWS_ENCRYPTION_KEY = base64.b64encode(password)

更新

我正在使用 python3。

【问题讨论】:

  • 我放弃了。似乎所有工作样本都在 java/nodejs 中。网络中的其他示例似乎不起作用。我认为您应该在 boto3 github github.com/boto/boto3 中打开一个问题,botocore 中可能存在未知错误。
  • 一天前我确实打开并发布了一个工作示例。仍然没有来自贡献者的回应。 github.com/boto/boto3/issues/684
  • @mootmoot 终于得到了贡献者的回复。现在有一个工作示例。

标签: python django encryption amazon-s3 boto3


【解决方案1】:

在 boto3 库上发布问题后,我终于得到了一个工作示例。这是应该如何完成的。

import boto3
import os

BUCKET = 'YOUR-BUCKET'
KEY = os.urandom(32)
s3 = boto3.client('s3')
print("Put object...")
s3.put_object(Bucket=BUCKET,
              Key='path_of_object_in_bucket', Body=b'foobar',
              SSECustomerKey=KEY,
              SSECustomerAlgorithm='AES256')
print("Done")
# Make sure to save the KEY!

# Getting the object:
print("Getting object...")
response = s3.get_object(Bucket=BUCKET,
                         Key='path_of_object_in_bucket',
                         SSECustomerKey=KEY,
                         SSECustomerAlgorithm='AES256')
print("Done, response body:")
print(response['Body'].read())

【讨论】:

  • 这是 32 字节的原始二进制文件,绝对不是 AWS 文档中所述的 base64 编码密钥。虽然 boto3 文档中没有提到 base64 编码的要求。
猜你喜欢
  • 2016-05-27
  • 1970-01-01
  • 2017-02-03
  • 2021-01-02
  • 1970-01-01
  • 2020-12-04
  • 1970-01-01
  • 2022-01-25
  • 2011-08-19
相关资源
最近更新 更多