Teach the AWS Lambda to use the [minified]-platforms database
[WebKit-https.git] / Tools / WebKitArchiveSupport / lambda / register-minified-s3-archive-in-dynamodb.py
1 import boto3
2 import json
3 import time
4 import urllib.parse  # pylint: disable=E0611
5
6 DAYS_TO_KEEP = 910
7 EPOCH_DAY = 86400
8 ARCHIVE_TABLE_NAME = 'minified-archives.webkit.org'
9 PLATFORM_TABLE_NAME = 'minified-platforms.webkit.org'
10 URL_PREFIX = 'https://s3-us-west-2.amazonaws.com'
11
12 dynamodb_client = boto3.client('dynamodb')
13 s3 = boto3.client('s3')
14
15
16 def lambda_handler(event, context):
17     print("Received event: " + json.dumps(event, indent=2))
18
19     # Get the bucket name from the event
20     bucket = event['Records'][0]['s3']['bucket']['name']
21     key = urllib.parse.unquote_plus(event['Records'][0]['s3']['object']['key'], encoding='utf-8')  # pylint: disable=E1101
22     # ex: mac-sierra-x86_64-debug/218331.zip
23
24     split_key = key.split('/')
25     identifier = split_key[0]           # mac-sierra-x86_64-debug
26     filename = split_key[1]             # 218331.zip
27     revision = filename.split('.')[0]   # 218331
28     creation_time = int(time.time())
29     expiration_time = creation_time + DAYS_TO_KEEP * EPOCH_DAY
30
31     creation_time = str(creation_time)
32     expiration_time = str(expiration_time)
33     s3_url = '/'.join([URL_PREFIX, bucket, key])
34
35     try:
36         item = {
37             'identifier': {'S': identifier},
38             'revision': {'N': revision},
39             's3_url': {'S': s3_url},
40             'creationTime': {'N': creation_time},
41             'expirationTime': {'N': expiration_time},
42         }
43         print('Item: {}'.format(item))
44         dynamodb_client.put_item(TableName=ARCHIVE_TABLE_NAME, Item=item)
45
46         platform = {
47             'identifier': {'S': identifier},
48             'creationTime': {'N': creation_time},
49             'expirationTime': {'N': expiration_time},
50         }
51         print('Item: {}'.format(platform))
52         dynamodb_client.put_item(TableName=PLATFORM_TABLE_NAME, Item=platform)
53
54         return s3_url
55     except Exception as e:
56         print(e)
57         print('Error registering item: {}\nfrom bucket {}.'.format(item, bucket))
58         raise e