Skip to content

Instantly share code, notes, and snippets.

@manavskohli
Last active February 13, 2019 18:32
Show Gist options
  • Save manavskohli/7157b9d973197c5096efba799cc42732 to your computer and use it in GitHub Desktop.
Save manavskohli/7157b9d973197c5096efba799cc42732 to your computer and use it in GitHub Desktop.
# Original source code:
# aws.amazon.com/blogs/compute/content-replication-using-aws-lambda-and-amazon-s3/
import urllib
import boto3
import ast
import json
print('Loading function')
def lambda_handler(event, context):
s3 = boto3.client('s3')
sns_message = ast.literal_eval(event['Records'][0]['Sns']['Message'])
target_bucket = 'target-bucket-two'
source_bucket = str(sns_message['Records'][0]['s3']['bucket']['name'])
key = str(urllib.unquote_plus(sns_message['Records'][0]['s3']['object']['key'])
.decode('utf8'))
copy_source = {'Bucket':source_bucket, 'Key':key}
# TODO: any other processing here
s3.copy_object(Bucket=target_bucket, Key=key, CopySource=copy_source)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment