Skip to content

Instantly share code, notes, and snippets.

@facundofarias
Created March 7, 2019 13:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save facundofarias/db355f8e42c6f0940a15542d0e3d60aa to your computer and use it in GitHub Desktop.
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
#####################################
# 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