Created
March 7, 2019 13:36
-
-
Save facundofarias/db355f8e42c6f0940a15542d0e3d60aa to your computer and use it in GitHub Desktop.
Use a AWS Lambda to send a notification when a file was added to AWS s3
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
##################################### | |
# Notify when a s3 file was created # | |
##################################### | |
import json | |
import boto3 | |
import urllib.parse | |
import urllib.request | |
print('Loading function') | |
s3 = boto3.client('s3') | |
def lambda_handler(event, context): | |
print("Received event: " + json.dumps(event, indent=2)) | |
bucket = event['Records'][0]['s3']['bucket']['name'] | |
key = urllib.parse.unquote_plus(event['Records'][0]['s3']['object']['key'], encoding='utf-8') | |
url = 'YOUR_URL' | |
values = { 'Key' : key } | |
try: | |
# Set the data to send | |
data = json.dumps(values) | |
data = data.encode('utf-8') | |
# Set the headers | |
headers = {} | |
headers['Content-Type'] = "application/json" | |
# Send the request | |
req = urllib.request.Request(url, data, headers) | |
resp = urllib.request.urlopen(req) | |
# Receive the response | |
respData = resp.read() | |
print(respData) | |
except Exception as e: | |
print(e) | |
print('Error getting object {} from bucket {}.'.format(key, bucket)) | |
raise e |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment