Skip to content

Instantly share code, notes, and snippets.

@ensean
Created August 25, 2023 05:19
Show Gist options
  • Save ensean/42d5c2df5b68476fc48e75d6a079833c to your computer and use it in GitHub Desktop.
Save ensean/42d5c2df5b68476fc48e75d6a079833c to your computer and use it in GitHub Desktop.
change-s3-content-type
import json
import urllib.parse
import boto3
print('Loading function')
s3 = boto3.client('s3')
def lambda_handler(event, context):
#print("Received event: " + json.dumps(event, indent=2))
# Get the object from the event and show its content type
bucket = event['Records'][0]['s3']['bucket']['name']
key = urllib.parse.unquote_plus(event['Records'][0]['s3']['object']['key'], encoding='utf-8')
try:
response = s3.get_object(Bucket=bucket, Key=key)
src = "%s/%s" % (bucket, key)
response_cpy = s3.copy_object(Bucket=bucket, CopySource=src, Key=key, ContentType='image/jpeg', MetadataDirective='REPLACE')
return response['ContentType']
except Exception as e:
print(e)
print('Error getting object {} from bucket {}. Make sure they exist and your bucket is in the same region as this function.'.format(key, bucket))
raise e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment