function serverless works!
This commit is contained in:
parent
f1b0ace7ae
commit
f31221caf0
40
eindopdracht/lambda/lambda_function.py
Normal file
40
eindopdracht/lambda/lambda_function.py
Normal 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
|
Reference in New Issue
Block a user