MCPcopy Create free account
hub / github.com/aws-samples/aws-cdk-examples / read_short_url

Function read_short_url

python/url-shortener/lambda/handler.py:63–90  ·  view source on GitHub ↗
(event)

Source from the content-addressed store, hash-verified

61
62
63def read_short_url(event):
64 # Parse redirect ID from path
65 id = event['pathParameters']['proxy']
66
67 # Pull out the DynamoDB table name from the environment
68 table_name = os.environ.get('TABLE_NAME')
69
70 # Load redirect target from DynamoDB
71 ddb = boto3.resource('dynamodb')
72 table = ddb.Table(table_name)
73 response = table.get_item(Key={'id': id})
74 LOG.debug("RESPONSE: " + json.dumps(response))
75
76 item = response.get('Item', None)
77 if item is None:
78 return {
79 'statusCode': 400,
80 'headers': {'Content-Type': 'text/plain'},
81 'body': 'No redirect found for ' + id
82 }
83
84 # Respond with a redirect
85 return {
86 'statusCode': 301,
87 'headers': {
88 'Location': item.get('target_url')
89 }
90 }

Callers 1

mainFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected