如何为bo3.s3.transfer.TransferConfig添加加密以上传s3文件

ajp*_*nam 7 encryption amazon-s3 amazon-web-services boto3

我正在尝试使用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')
Run Code Online (Sandbox Code Playgroud)

我现在想使用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)
Run Code Online (Sandbox Code Playgroud)

boto3文档

ajp*_*nam 6

在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'}
)
Run Code Online (Sandbox Code Playgroud)

使用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'})
Run Code Online (Sandbox Code Playgroud)