Skip to content

Instantly share code, notes, and snippets.

@nqbao
Created February 13, 2020 21:36
Show Gist options
  • Save nqbao/8fa1074a7ea71b381c27b78cfda53c42 to your computer and use it in GitHub Desktop.
Save nqbao/8fa1074a7ea71b381c27b78cfda53c42 to your computer and use it in GitHub Desktop.
Snippet to handle s3 events
import boto3
import json
import urllib.parse
import uuid
import os
def lambda_handler(event, context):
sfn = boto3.client('stepfunctions')
for record in event.get('Records', []):
if "ObjectCreated" in record['eventName']:
bucket = record['s3']['bucket']['name']
key = urllib.parse.unquote_plus(record['s3']['object']['key'])
if key[-1] == "/":
continue
try:
sfn.start_execution(
stateMachineArn=os.environ['SFN_SM_ARN'],
name="lambda-{}".format(str(uuid.uuid4())),
input=json.dumps({
"mode": "url",
"url": "s3://{}/{}".format(bucket, key)
})
)
except Exception as ex:
print("Unable to start execution: {}".format(ex))
else:
print("Ignore event {}", record['eventName'])
return {
'statusCode': 200,
'body': 'OK'
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment