AWS | Project | Final Part | Read S3 CSV file and insert into RDS mysql using Python Lambda Function

Published: 20 September 2020
on channel: Cloud Tech Masters
12,919
125

import json
import csv
import boto3
import mysql.connector

s3client=boto3.client('s3')
def lambda_handler(event, context):
bucket = event['Records'][0]['s3']['bucket']['name']
csv_file = event['Records'][0]['s3']['object']['key']
response = s3client.get_object(Bucket=bucket, Key=csv_file)
lines = response['Body'].read().decode('utf-8').split()
results = []
for row in csv.DictReader(lines):
results.append(row.values())
print(results)

connection = mysql.connector.connect(host='mysql.c79sd2kyheg7.us-east-1.rds.amazonaws.com',
database='employeedb',
port='3306',
user='admin',
passwd='admin123')

mysql_insert = "insert into employee(empid,ename,salary) values(%s,%s,%s)"

cursor = connection.cursor()
cursor.executemany(mysql_insert, results)
connection.commit()

return {
'statusCode': 200,
'body': json.dumps('Hello from Lambda!')
}


On this page of the site you can watch the video online AWS | Project | Final Part | Read S3 CSV file and insert into RDS mysql using Python Lambda Function with a duration of hours minute second in good quality, which was uploaded by the user Cloud Tech Masters 20 September 2020, share the link with friends and acquaintances, this video has already been watched 12,919 times on youtube and it was liked by 125 viewers. Enjoy your viewing!