-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathversion3.py
More file actions
52 lines (42 loc) · 1.63 KB
/
Copy pathversion3.py
File metadata and controls
52 lines (42 loc) · 1.63 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import json
import csv
import boto3
def lambda_handler(event, context):
region = 'eu-west-2'
record_list = []
try:
s3 = boto3.client('s3')
dynamodb = boto3.client('dynamodb', region_name = region)
bucket = event['Records'][0]['s3']['bucket']['name']
key = event['Records'][0]['s3']['object']['key']
print('Bucket: ', bucket, '\nKey:',key)
csv_file = s3.get_object(Bucket = bucket, Key= key)
record_list = csv_file['Body'].read().decode('utf-8').split('\n')
csv_reader = csv.reader(record_list, delimiter = ',', quotechar = '"')
for row in csv_reader:
movie_id = row[0]
movie = row[1]
title = row[2]
year = row[3]
print('\nMovie_ID: ', movie_id, '\nMovie: ', movie, '\nTitle: ', title, '\nYear: ', year)
add_db = dynamodb.put_item(
TableName = 'playground-db-silly-panda',
Item = {
'movie_id' : {'N' : str(movie_id)},
'movie' : {'S' : str(movie)},
'title' : {'S' : str(title)},
'year' : {'N' : str(year)}
}
)
print('\nSuccessfully added the records to the DynamoDB Table!')
except Exception as e:
print(str(e))
return {
'statusCode': 500,
'body': json.dumps('Oh no, something went wrong!')
}
else:
return {
'statusCode': 200,
'body': json.dumps('Success!')
}