-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaws-lambda-consume.py
37 lines (30 loc) · 1.01 KB
/
aws-lambda-consume.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import json
import boto3, botocore
def lambda_handler(event, context):
headers = event.get('headers')
batch_id = headers.get('x-messagesystems-batch-id')
body = event.get('body')
# store file in S3
s3_client = boto3.client('s3')
store_batch(s3_client, body, batch_id)
return {
'statusCode': 200,
'body': 'done'
}
def store_batch(s3_client, body, batch_id):
bucket_name = 'sparkpost-webhooks'
path = 'SP_Event_Data_test/' + str(batch_id)
try:
try:
_ = s3_client.get_object(Bucket=bucket_name, Key=path)
return
except botocore.exceptions.ClientError as err:
e = err.response['Error']['Code']
if e in ['NoSuchKey', 'AccessDenied']:
# Forward path. Object does not exist already, so try to create it
s3_client.put_object(Body=body, Bucket=bucket_name, Key=path)
else:
print(err)
except Exception as err:
print(err)
return