Skip to content

Instantly share code, notes, and snippets.

@overdrive3000
Created April 27, 2016 01:31
Show Gist options
  • Save overdrive3000/a1e65a7348374daaef729e813f454258 to your computer and use it in GitHub Desktop.
Save overdrive3000/a1e65a7348374daaef729e813f454258 to your computer and use it in GitHub Desktop.
from __future__ import print_function
import json
import urllib
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.unquote_plus(event['Records'][0]['s3']['object']['key']).decode('utf8')
try:
response = s3.get_object(Bucket=bucket, Key=key)
print("Last Modified ", response['LastModified'])
new_key = "to_backup/{}".format(key.split('/')[1])
old_key = "{}/{}".format(bucket, key)
print("Copying {} to {}/{}".format(old_key, bucket, new_key))
resource = boto3.resource('s3')
resource.Object(bucket, new_key).copy_from(CopySource=old_key)
return 'OK'
except Exception as e:
print(e)
raise e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment