我正在尝试使用boto3 file_upload方法将文件上传到s3。这很简单,直到需要服务器端加密为止。过去,我曾使用put_object实现这一目标。
像这样:
import boto3
s3 = boto3.resource('s3')
s3.Bucket(bucket).put_object(Key=object_name,
Body=data,
ServerSideEncryption='aws:kms',
SSEKMSKeyId='alias/aws/s3')
现在,我想使用file_upload方法将文件直接上传到s3。我找不到如何将服务器端加密添加到file_upload方法。 file_upload方法可以采用TransferConfig,但是我看不到任何设置加密的参数,但是我确实在S3Transfer中看到了它们。
我正在寻找这样的东西:
import boto3
s3 = boto3.resource('s3')
tc = boto3.s3.transfer.TransferConfig(ServerSideEncryption='aws:kms',
SEKMSKeyId='alias/aws/s3')
s3.upload_file(file_name,
bucket,
object_name,
Config=tc)
boto3文档
最佳答案
在jarmod的帮助下,我能够提出两种解决方案。
使用boto3.s3.transfer.S3Transfer
import boto3
client = boto3.client('s3', 'us-west-2')
transfer = boto3.s3.transfer.S3Transfer(client=client)
transfer.upload_file(file_name,
bucket,
key_name,
extra_args={'ServerSideEncryption':'aws:kms',
'SSEKMSKeyId':'alias/aws/s3'}
)
使用s3.meta.client
import boto3
s3 = boto3.resource('s3')
s3.meta.client.upload_file(file_name,
bucket, key_name,
ExtraArgs={'ServerSideEncryption':'aws:kms',
'SSEKMSKeyId':'alias/aws/s3'})
关于amazon-web-services - 如何在boto3.s3.transfer.TransferConfig中添加加密以上传s3文件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46610550/