Created
February 13, 2020 21:36
-
-
Save nqbao/8fa1074a7ea71b381c27b78cfda53c42 to your computer and use it in GitHub Desktop.
Snippet to handle s3 events
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 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