-
Notifications
You must be signed in to change notification settings - Fork 0
/
s3_csv_to_dynamodb.py
35 lines (28 loc) · 1.36 KB
/
s3_csv_to_dynamodb.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
35
# Persist CSV data to DynamoDB when file is uploaded to an S3 bucket
import boto3
s3_client = boto3.client('s3')
dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('employees')
def s3_csv_to_dynamodb(event, context):
# get uploaded file from event response details
bucket_name = event['Records'][0]['s3']['bucket']['name']
object_key = event['Records'][0]['s3']['object']['key']
response = s3_client.get_object(Bucket=bucket_name, Key=object_key)
# read contents of CSV file & prepare data for DynamoDB
csv_data = response['Body'].read() # call read() method because body is returned as a StreamingBody()
csv_formatted = csv_data.decode('utf-8') # call decode() to convert from binary form to plain text
employees = csv_formatted.split('\n') # create a list to iterate over
# persist data to DynamoDB (i.e. enter data in table)
for emp in employees:
emp_data = emp.split(',') # create a list to reference individual attributes via indices (match to original CSV contents)
try:
table.put_item(
Item={
'id': emp_data[0],
'name': emp_data[1],
'location': emp_data[2]
}
)
except Exception as e:
print(f'Error: not able to add item {emp} to DynamoDB')
return None