在 python 中为 boto3 文件加密创建 SSECustomerKey 的正确方法是什么?
What is the right way to create a SSECustomerKey for boto3 file encryption in python?
我在我的 django 应用程序中使用 boto3 将媒体上传到 S3。但是我在使用 "Server Side Encryption using Customer Provided Encryption Keys"
加密服务器上的文件时遇到问题
我正在使用 boto3 的 object.put() api 上传文件并指定加密密钥。但是我收到以下错误。
"The calculated MD5 hash of the key did not match the hash that was
provided."
我不确定如何创建将在服务器端匹配的密钥的 md5。这是我的代码。
password = "32characterslongpassphraseneeded".encode('utf-8')
encryption_key = hashlib.md5(password).hexdigest()
encryption_key_md5 = hashlib.md5(encryption_key.encode('utf-8')).hexdigest()
import boto3
s3 = boto3.resource('s3')
key = s3.Object(bucket_name, key_name)
kwargs = {
'SSECustomerAlgorithm': 'AES256',
'SSECustomerKey': encryption_key,
'SSECustomerKeyMD5': encryption_key_md5,
'ContentType': file_obj.content_type,
'Body': file_obj,
}
key.put(**kwargs)
我通过 php 客户端使用相同的 s3 api,它工作正常。
$name="somename"
$customerKey = md5($name);
$s3->putObject([
'Bucket' => S3_BUCKET,
'Key' => "scope/{$name}",
'Body' => fopen($tmp_file_path, 'rb'),
'ACL' => S3_ACL,
'SSECustomerAlgorithm' => 'AES256',
'SSECustomerKey' => $customerKey,
'SSECustomerKeyMD5' => md5($customerKey ,true),
]);
我在这里看到的唯一区别是 php 的 md5 方法可以采用第二个参数,如果为真,returns 和 16 个字符长的摘要与正常的 32 个字符长的摘要相比。现在我不知道如何使用 hashlib.md5 创建一个 16 个字符长的摘要。
正确的做法是使用os.urandom
import os
secret_key = os.urandom(32) # The key needs to be 32 character long.
并且不需要提供 SSECustomerKeyMD5
,因为 boto3 会为您计算。
而且 SSE-C 在 key.put
中不能正常工作,至于现在,我不知道是什么原因。必须这样做。
s3 = boto3.client('s3')
s3.put_object(**kwargs)
SSE 似乎也适用于 Object
。举例如下
import boto3
from botocore.config import Config
s3 = boto3.resource('s3',
region_name="us-east-1",
aws_access_key_id="key id",
aws_secret_access_key="access key",
config=Config(signature_version='s3v4'))
s3.Object("bucket", "filename").put(Body="text",
SSEKMSKeyId="some id",
ServerSideEncryption='aws:kms')
要阅读它,请使用
s3.Object("bucket", "filename").get()['Body'].read()
我在我的 django 应用程序中使用 boto3 将媒体上传到 S3。但是我在使用 "Server Side Encryption using Customer Provided Encryption Keys"
加密服务器上的文件时遇到问题我正在使用 boto3 的 object.put() api 上传文件并指定加密密钥。但是我收到以下错误。
"The calculated MD5 hash of the key did not match the hash that was provided."
我不确定如何创建将在服务器端匹配的密钥的 md5。这是我的代码。
password = "32characterslongpassphraseneeded".encode('utf-8')
encryption_key = hashlib.md5(password).hexdigest()
encryption_key_md5 = hashlib.md5(encryption_key.encode('utf-8')).hexdigest()
import boto3
s3 = boto3.resource('s3')
key = s3.Object(bucket_name, key_name)
kwargs = {
'SSECustomerAlgorithm': 'AES256',
'SSECustomerKey': encryption_key,
'SSECustomerKeyMD5': encryption_key_md5,
'ContentType': file_obj.content_type,
'Body': file_obj,
}
key.put(**kwargs)
我通过 php 客户端使用相同的 s3 api,它工作正常。
$name="somename"
$customerKey = md5($name);
$s3->putObject([
'Bucket' => S3_BUCKET,
'Key' => "scope/{$name}",
'Body' => fopen($tmp_file_path, 'rb'),
'ACL' => S3_ACL,
'SSECustomerAlgorithm' => 'AES256',
'SSECustomerKey' => $customerKey,
'SSECustomerKeyMD5' => md5($customerKey ,true),
]);
我在这里看到的唯一区别是 php 的 md5 方法可以采用第二个参数,如果为真,returns 和 16 个字符长的摘要与正常的 32 个字符长的摘要相比。现在我不知道如何使用 hashlib.md5 创建一个 16 个字符长的摘要。
正确的做法是使用os.urandom
import os
secret_key = os.urandom(32) # The key needs to be 32 character long.
并且不需要提供 SSECustomerKeyMD5
,因为 boto3 会为您计算。
而且 SSE-C 在 key.put
中不能正常工作,至于现在,我不知道是什么原因。必须这样做。
s3 = boto3.client('s3')
s3.put_object(**kwargs)
SSE 似乎也适用于 Object
。举例如下
import boto3
from botocore.config import Config
s3 = boto3.resource('s3',
region_name="us-east-1",
aws_access_key_id="key id",
aws_secret_access_key="access key",
config=Config(signature_version='s3v4'))
s3.Object("bucket", "filename").put(Body="text",
SSEKMSKeyId="some id",
ServerSideEncryption='aws:kms')
要阅读它,请使用
s3.Object("bucket", "filename").get()['Body'].read()