Skip to content

Instantly share code, notes, and snippets.

@pavelanni
Last active June 7, 2018 17:32
Show Gist options
  • Save pavelanni/da26dc5d2ec17801830db482a3003533 to your computer and use it in GitHub Desktop.
Save pavelanni/da26dc5d2ec17801830db482a3003533 to your computer and use it in GitHub Desktop.
Lambda function to automatically copy objects uploaded to S3 bucket to a Glacier vault. It also creates a record in DynamoDB for each archived object so they can be retrieved later
import urllib.parse
import boto3
import tempfile
from datetime import datetime
account_id = 'XXXXXXXXXX'
db_name = 'archive-db'
vault = 'archive-vault'
def lambda_handler(event, context):
s3 = boto3.resource('s3')
glacier = boto3.client('glacier')
ddb = boto3.resource('dynamodb')
table = ddb.Table(db_name)
bucket = event['Records'][0]['s3']['bucket']['name']
key = key = urllib.parse.unquote_plus(event['Records'][0]['s3']['object']['key'], encoding='utf-8')
tmpfile = tempfile.TemporaryFile()
try:
s3.Object(bucket, key).download_fileobj(tmpfile)
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
filesize = tmpfile.tell()
tmpfile.seek(0)
response = glacier.upload_archive(vaultName=vault,
archiveDescription=key,
body=tmpfile)
db_response = table.put_item(Item={'archiveId': response['archiveId'],
'object': key,
'size': filesize,
'archived': datetime.today().isoformat(),
'bucket': bucket,
'location': response['location'],
'checksum': response['checksum'],
}
)
return response['archiveId']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment