2017-10-06 120 views
2

我想上传一个文件到S3使用boto3 file_upload方法。这是非常直接的,直到需要服务器端加密。在过去,我使用put_object来实现这一点。如何添加加密到boto3.s3.transfer.TransferConfig s3文件上传

像这样:

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文档

+1

如果我读这正确,第四参数S3.Client.upload_file ()似乎是extraArgs。你能够提供ExtraArgs = {ServerSideEncryption:'aws:kms',SSEKMSKeyId:'alias/aws/s3'}。请注意SSEKMSKeyId(不是SEKMSKeyId)的拼写。 – jarmod

回答

1

我能够拿出来与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'})