-
Notifications
You must be signed in to change notification settings - Fork 4
/
handler.py
34 lines (29 loc) · 1.05 KB
/
handler.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import boto3
import re
def read_file(s3_client, bucket, key):
return s3_client.get_object(
Bucket=bucket,
Key=key
)['Body'].read().decode('utf-8')
def move_object_to_processed(s3_client, original_bucket, original_key):
new_key = re.sub("incoming\/", "processed/", original_key)
s3_client.copy_object(
Bucket=original_bucket,
Key=new_key,
CopySource={'Bucket': original_bucket, 'Key': original_key}
)
s3_client.delete_object(Bucket=original_bucket, Key=original_key)
def call(event, context):
s3_client = boto3.client('s3')
record = event['Records'][0]
bucket = record['s3']['bucket']['name']
key = record['s3']['object']['key']
table = boto3.resource('dynamodb', region_name='us-east-1').Table("my-transactions-table")
txn_id = re.search("incoming\/transaction-(\d*).txt", key).group(1)
table.put_item(
Item={
'transaction_id': txn_id,
'body': read_file(s3_client, bucket, key)
}
)
move_object_to_processed(s3_client, bucket, key)