Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to parse CSVs from s3 bucket to use in a javascript AWS Lambda function

I'm trying to read in a CSV from an s3 bucket using the csvtojson library in AWS Lambda, but it's not working properly. Locally, my code works. But when I upload it to Lambda, it doesn't return anything. There are no errors in the Lambda console, so I'm having a hard time debugging. My code is below.

const AWS = require('aws-sdk');
const csvtojson = require('csvtojson');

const s3 = new AWS.S3();

const params = {
   Bucket: bucketName,
   Key: pathToFile 
};

const stream = s3.getObject(params).createReadStream();

csvtojson()
   .fromStream(stream)
   .then((json) => {
     console.log('Locally, this returns the CSV as JSON. On Lambda, it does not.');
   });

Does csvtojson not work on Lambda for some reason? Should I be using a different method to parse the CSV? Thanks!

like image 719
michaelvhester Avatar asked Dec 21 '25 15:12

michaelvhester


1 Answers

Your lambda is finishing before the Promise has completed. Replace the last section with this:

const json = await csvtojson().fromStream(stream);
console.log('Locally, this returns the CSV as JSON. On Lambda, it does not.');
like image 50
Joey Kilpatrick Avatar answered Dec 24 '25 08:12

Joey Kilpatrick



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!