Archived
1

function serverless works!

This commit is contained in:
ventilaar 2022-04-05 15:31:38 +02:00
parent f1b0ace7ae
commit f31221caf0

View File

@ -0,0 +1,40 @@
import json
import boto3
import base64
def lambda_handler(event, context):
method = str(event.get('requestContext').get('http').get('method'))
dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('lambdatable')
if method == "POST":
key = str(event.get('pathParameters').get('id'))
url = str(base64.b64decode(event.get('body')), 'utf-8')
table.put_item(Item={'key': key, 'url': url})
return {'message': url}
elif method == "DELETE":
key = str(event.get('pathParameters').get('id'))
table.delete_item(Key={'key': key})
return {'message': key}
else:
key = str(event.get('pathParameters').get('id'))
data = table.get_item(Key={"key": key})
if len(data) is 1:
return {'message': 'No such key'}
response = {
"statusCode": 302,
"headers": {
'Location': data['Item']['url']
}
}
return response