How do I read a csv file from aws s3 in aws lambda
Question:
I am new to the AWS services.I am trying to read a csv file from AWS S3 bucket but I cannot read or get any output from AWS lambda.
import json
import boto3
import csv
s3_client = boto3.client('s3')
def lambda_handler(event, context):
# TODO implement
try:
bucket_name = event["Records"][0]["s3"]["bucket"]["name"]
s3_file_name = event["Records"][0]["s3"]["object"]["key"]
csv_file = s3_client.get_object(Bucket=bucket_name, Key=s3_file_name)
data = csv_file['Body'].read().splitlines(True)
reader = csv.reader(data)
print(reader)
except Exception as err:
print(err)
return {
'statusCode': 200,
'body': json.dumps('Hello from Lambda!')
}
I am getting this output but I don’t get data from my print line why is that?
please help me!
Answers:
https://docs.python.org/3/library/csv.html
According to the documents, I think you used the wrong way of csv module. So the reader is empty and that’s why your code does not return anything
An easy way to read csv is by using pandas.
import boto3
import pandas as pd
s3 = boto3.client('s3')
response = s3.get_object(Bucket=bucket, Key=path)
content = pd.read_csv(response.get("Body"))
I am new to the AWS services.I am trying to read a csv file from AWS S3 bucket but I cannot read or get any output from AWS lambda.
import json
import boto3
import csv
s3_client = boto3.client('s3')
def lambda_handler(event, context):
# TODO implement
try:
bucket_name = event["Records"][0]["s3"]["bucket"]["name"]
s3_file_name = event["Records"][0]["s3"]["object"]["key"]
csv_file = s3_client.get_object(Bucket=bucket_name, Key=s3_file_name)
data = csv_file['Body'].read().splitlines(True)
reader = csv.reader(data)
print(reader)
except Exception as err:
print(err)
return {
'statusCode': 200,
'body': json.dumps('Hello from Lambda!')
}
I am getting this output but I don’t get data from my print line why is that?
please help me!
https://docs.python.org/3/library/csv.html
According to the documents, I think you used the wrong way of csv module. So the reader is empty and that’s why your code does not return anything
An easy way to read csv is by using pandas.
import boto3
import pandas as pd
s3 = boto3.client('s3')
response = s3.get_object(Bucket=bucket, Key=path)
content = pd.read_csv(response.get("Body"))