| 30 | |
| 31 | |
| 32 | def create_short_url(event): |
| 33 | # Pull out the DynamoDB table name from environment |
| 34 | table_name = os.environ.get('TABLE_NAME') |
| 35 | |
| 36 | # Parse targetUrl |
| 37 | target_url = event["queryStringParameters"]['targetUrl'] |
| 38 | |
| 39 | # Create a unique id (take first 8 chars) |
| 40 | id = str(uuid.uuid4())[0:8] |
| 41 | |
| 42 | # Create item in DynamoDB |
| 43 | dynamodb = boto3.resource('dynamodb') |
| 44 | table = dynamodb.Table(table_name) |
| 45 | table.put_item(Item={ |
| 46 | 'id': id, |
| 47 | 'target_url': target_url |
| 48 | }) |
| 49 | |
| 50 | # Create the redirect URL |
| 51 | url = "https://" \ |
| 52 | + event["requestContext"]["domainName"] \ |
| 53 | + event["requestContext"]["path"] \ |
| 54 | + id |
| 55 | |
| 56 | return { |
| 57 | 'statusCode': 200, |
| 58 | 'headers': {'Content-Type': 'text/plain'}, |
| 59 | 'body': "Created URL: %s" % url |
| 60 | } |
| 61 | |
| 62 | |
| 63 | def read_short_url(event): |