Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

S3 upload using server-side encryption (python SDK)

I'm using the following snippet to upload my files to the AWS S3 buckets:

import boto3

def upload_to_s3(bucket_name, local_name, name):
    bucket = boto3.resource('s3').Bucket(my_bucket_name)
    bucket.upload_file(local_name, name)

Is there any way to modify this code to enable SSE?

like image 436
Rail Suleymanov Avatar asked May 03 '26 10:05

Rail Suleymanov


1 Answers

There are 2 ways.

  1. use this: https://www.justdocloud.com/2018/09/21/upload-download-s3-using-aws-kms-python/

    s3_client.upload_file(filename, bucketname, objectkey, ExtraArgs={"ServerSideEncryption": "aws:kms", "SSEKMSKeyId": })
    
  2. Enable Default bucket encryption with KMS on bucket and make sure the user/role you're using to upload has KMS permission, this way you don't need to define any kms key here.

like image 159
James Dean Avatar answered May 05 '26 01:05

James Dean