Ignore Default AWS KMS Encryption for S3 Uploads Using Python Boto3 - python-3.x

We recently enabled AWS KMS for all of our Amazon S3 buckets which, by default, applies server-side encryption to all files we upload to our own S3 buckets or to S3 buckets owned by someone else.
Is there a way to intentionally "ignore" the default KMS encryption to upload unencrypted files to an S3 bucket owned by a 3rd party? The 3rd party team cannot open any of the files we are sending them. I understand that one solution would be to share the KMS key with the 3rd party but, due to the nature of the relationship, it's better if we only deliver unencrypted files instead of sharing a key.
Here is the Python code I have been using to deliver the files. How can I modify the ExtraArgs parameter to intentionally ignore the default KMS encryption?
from boto3 import client
from boto3.s3.transfer import TransferConfig
client = client('s3', ...)
config = TransferConfig(multipart_threshold=1024 * 25, multipart_chunksize=1024 * 25,
max_concurrency=10, use_threads=True)
client.upload_file(filename='test.csv', bucket='my-bucket', key='test.csv',
Config=config, ExtraArgs={'ACL': 'bucket-owner-full-control'})

Related

Use aws-encryption-sdk to decrypt with context in on-prem environment

I want to consume encrypted messages using the aws_encryption_sdk python module. The messages are encrypted by another team using a context.
The reason to use aws_encryption_sdk is to reduce the cost by reducing the number of KMS sessions whenever we call the boto3 kms client decrypt function. However, I seem to be stuck and confused as I can't find where I can use that when calling the decrypt function for the EncryptionSDKClient
My code looks something like this:
client = aws_encryption_sdk.EncryptionSDKClient()
## try to set the botocore session for Master Key Provider
kms_kwargs= dict(key_ids=data['keyId'])
key_provider = aws_encryption_sdk.StrictAwsKmsMasterKeyProvider(**kms_kwargs)
MAX_ENTRY_AGE_SECONDS = 600.0
MAX_ENTRY_MESSAGES = 10
MAX_CACHE_SIZE = 10
cache = aws_encryption_sdk.LocalCryptoMaterialsCache(MAX_CACHE_SIZE)
caching_cmm = CachingCryptoMaterialsManager(
master_key_provider=key_provider,
cache=cache,
max_age=MAX_ENTRY_AGE_SECONDS,
max_messages_encrypted=MAX_ENTRY_MESSAGES
)
cycled_plaintext, decrypted_header = client.decrypt(source=base64.b64decode(data["encryptedData"]), key_provider=key_provider)
Please note that I need to cache the data key in on-premise environment.
From the AWS Developer Guide on KMS Encryption SDK page, encryption context section:
To decrypt the data, you pass in the encrypted message. Because the AWS Encryption SDK can extract the encryption context from the encrypted message header, you are not required to provide the encryption context separately. However, the encryption context can help you to confirm that you are decrypting the correct encrypted message.
There is also a few examples of code verifying the encryption contexts that you may find useful here.

I am not able to read dat file from S3 bucket using lambda function

I have been trying to read dat file from one s3 bucket and convert it into CSV and then compress it and put it into another bucket
for open and reading i am using below code but it is throwing me an error No such file or directory
with open(f's3://{my_bucket}/{filenames}', 'rb') as dat_file:
print(dat_file)'''
The Python language does not natively know how to access Amazon S3.
Instead, you can use the boto3 AWS SDK for Python. See: S3 — Boto 3 documentation
You also have two choices about how to access the content of the file:
Download the file to your local disk using download_file(), then use open() to access the local file, or
Use get_object() to obtain a StreamingBody of the file contents
See also: Amazon S3 Examples — Boto 3 documentation

how to delete S3 particular prefix life cycle from python3

I am trying to delete the particular prefix life cycle by using python boto3
I have tried the below code, but below code is deleting the entire bucket life cycle configuration.
import boto3
client = boto3.client('s3')
response = client.delete_bucket_lifecycle(Bucket='my_bucket_name')
I want to delete the particular prefix life cycle.
The delete_bucket_policy() API call will delete a Bucket Policy, which is used to grant access to an Amazon S3 bucket.
It seems that you actually wish to delete a Lifecycle policy, which can be done with the delete_bucket_lifecycle() API call.

How to upload downloaded file to s3 bucket using Lambda function

I saw different questions/answers but I could not find the one that worked for me. Hence, I am really new to AWS, I need your help. I am trying to download gzip file and load it to the json file then upload it to the S3 bucket using Lambda function. I wrote the code to download the file and convert it to json but having problem while uploading it to the s3 bucket. Assume that file is ready as x.json. What should I do then?
I know it is really basic question but still help needed :)
This code will upload to Amazon S3:
import boto3
s3_client = boto3.client('s3', region_name='us-west-2') # Change as appropriate
s3._client.upload_file('/tmp/foo.json', 'my-bucket', 'folder/foo.json')
Some tips:
In Lambda functions you can only write to /tmp/
There is a limit of 512MB
At the end of your function, delete the files (zip, json, etc) because the container can be reused and you don't want to run out of disk space
If your lambda has proper permission to write a file into S3, then simply use boto3 package which is an AWS SDK for python.
https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/s3.html
Be aware that if the lambda locates inside of VPC then lambda cannot access to the public internet, and also boto3 API endpoint. Thus, you may require a NAT gateway to proxy lambda to the public.

Amazon S3 and Cloudfront - Publish file uploaded as hashed filename

Technologies:
Python3
Boto3
AWS
I have a project built using Python3 and Boto3 to communicate with a bucket in Amazon S3 service.
The process is that a user posts images to the service; these' images are uploaded to an S3 bucket, and can be served through amazon cloudfront using a hashed file name instead of the real file name.
Example:
(S3) Upload key: /category-folder/png/image.png
(CloudFront) Serve: http://d2949o5mkkp72v.cloudfront.net/d824USNsdkmx824
I want to file uploaded to S3, appear as hash number as file name in cloudfront server.
Does anyone have knowledge that makes S3 or cloudfront automatically convert and publish a file-name to a hash name.
In order to suffice my needs I created the fields needed to maintain the keys (to make them unique; both on S3 and in my mongodb)
Fields:
original_file_name = my_file_name
file_category = my_images, children, fun
file_type = image, video, application
key = uniqueID
With the mentioned fields; then one can check if the key exists by simply searching for the key, the new file_name, the category, and the type; if it exists in the database then file exists.
To generate the unique id:
def get_key(self):
from uuid import uuid1
return uuid1().hex[:20]
This limits the ID to the length of 20 characters.

Resources