Last active
June 4, 2018 02:49
-
-
Save JoeReis/249fde3f515b845f4b053438091e3c5c to your computer and use it in GitHub Desktop.
AWS Lambda function that creates metadata about a file uploaded to S3, and inserts this metadata into DynamoDB.
This file contains 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
import json | |
import urllib.parse | |
import boto3 | |
s3 = boto3.client('s3') | |
dynamodb_client = boto3.resource('dynamodb') | |
table_name = # your dynamodb table name | |
table = dynamodb_client.Table(table_name) | |
def lambda_handler(event, context): | |
# 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') | |
timestamp = event['Records'][0]['eventTime'] | |
size = event['Records'][0]['s3']['object']['size'] | |
try: | |
table.put_item(Item = { | |
'filename': key, | |
'bucket': bucket, | |
'timestamp': timestamp, | |
'size': size | |
}) | |
return "Dynamodb insert a success for file {}".format(key) | |
except Exception as 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