import json import os import csv import boto3 import pandas as pd def lambda_handler(event, context): # Writes the MvWarMain data to DynamoDB s3client = boto3.client('s3') s3 = boto3.resource('s3') print('Event:' + str(event)) print('Event-Records:' + str(event['Records'])) print('Event-Records-S3:'+ str(event['Records'][0]['s3'])) print('Event-Records-S3-bucket-name:'+ str(event['Records'][0]['s3']['bucket']['name'])) input_bucket = event['Records'][0]['s3']['bucket']['name'] input_key = event['Records'][0]['s3']['object']['key'] os.chdir('/tmp/') input_file_list = input_key.split('/') input_file = input_file_list[1] input_file_path = '/tmp/' + str(input_file) print("File:" + str(input_file)) print("File Path" + '/tmp/' + str(input_file)) #response = s3client.get_object(Bucket=input_bucket, Key=input_key) s3client.download_file(input_bucket, input_key, input_file_path) os.chdir('/tmp/') dynamo = boto3.resource('dynamodb') table = dynamo.Table(os.environ['DynamoDbTable']) print (os.listdir()) well_activity = pd.read_csv(input_file, delimiter = ',') for index, row in well_activity.iterrows(): response = table.put_item( Item={'SN_WAR':str(row['SN_WAR']), 'API_WELL_NUMBER':str(row['API_WELL_NUMBER']), 'WAR_START_DT':str(row['WAR_START_DT']), 'WAR_END_DT':str(row['WAR_END_DT']), 'CONTACT_NAME':str(row['CONTACT_NAME']), 'BUS_ASC_NAME':row['BUS_ASC_NAME'], 'MMS_COMPANY_NUM':str(row['MMS_COMPANY_NUM']), 'WELL_NAME':str(row['WELL_NAME']) }) return { 'Message': 'success' }