forked from starrohan999/Hacktoberfest-Accepted
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCreate S3_To_DynamoDB.py
32 lines (28 loc) · 1.07 KB
/
Create S3_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
"""
This code has been designed by - Amrit Raj
The code uses the boto3 to read the contents of a .txt file stored in a S3 bucket in AWS
and transfer the contents to a DynamoDB table at AWS.
This code is for example purpose and can be suitably modified for any .txt or other file format
and for any table.
For this example, we have assumed an employee table with three attributes - id, name, and salary
"""
import boto3
client = boto3.client("s3")
dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('employee')
def lambda_handler(event, context):
bucket_name = event['Records'][0]['s3']['bucket']['name']
file_name = event['Records'][0]['s3']['object']['key']
repo = client.get_object(Bucket = bucket_name, Key = file_name)
data = repo['Body'].read().decode("utf-8")
employees = data.split("\n")
for emp in employees:
print(emp)
emp_data = emp.split(", ")
table.put_item(
Item = {
"id" : int(emp_data[0]),
"name" : emp_data[1],
"salary" : emp_data[2]
}
)