| 61 | |
| 62 | |
| 63 | def 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 | } |