Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Burekasim/597981b5792946750ac8ed4b3bd8003b to your computer and use it in GitHub Desktop.
Save Burekasim/597981b5792946750ac8ed4b3bd8003b to your computer and use it in GitHub Desktop.
AWS Lamdba python 3.8 script that automatically adds public Cache-Control for 1 year. don't forget to grant iam permission and s3 event trigger.
import json
import boto3
from botocore.exceptions import ClientError
def lambda_handler(event, context):
bucket_name = event['Records'][0]['s3']['bucket']['name']
s3_file = event['Records'][0]['s3']['object']['key']
print(bucket_name, s3_file)
s3 = boto3.client('s3')
try:
t = s3.copy_object(Key=s3_file, Bucket=bucket_name,
CopySource={"Bucket": bucket_name, "Key": s3_file},
CacheControl="max-age=31536000, public",
MetadataDirective="REPLACE")
print(t)
except ClientError as e:
raise(e)
lambda_output = {'status': 200}
return lambda_output
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment