Skip to content

Instantly share code, notes, and snippets.

@JoeReis
Last active June 4, 2018 02:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JoeReis/249fde3f515b845f4b053438091e3c5c to your computer and use it in GitHub Desktop.
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.
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