Created
April 27, 2016 01:31
-
-
Save overdrive3000/a1e65a7348374daaef729e813f454258 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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