Read and parse CSV file in S3 without downloading the entire file
I do not have enough reputation to comment but as of now the accepted answer method "fromStream" is deprecated on 'fast-csv'. Now you'll need to use the parseStream method:
const s3Stream = s3.getObject(params).createReadStream()
require('fast-csv').parseStream(s3Stream)
.on('data', (data) => {
// use rows
})
You should just be able to use the createReadStream
method and pipe it into fast-csv:
const s3Stream = s3.getObject(params).createReadStream()
require('fast-csv').fromStream(s3Stream)
.on('data', (data) => {
// do something here
})
For me, the answer that solved my issue was,
const csv = require('@fast-csv/parse');
const params = {
Bucket: srcBucket,
Key: srcKey,
};
const csvFile = s3.getObject(params).createReadStream();
let parserFcn = new Promise((resolve, reject) => {
const parser = csv
.parseStream(csvFile, { headers: true })
.on("data", function (data) {
console.log('Data parsed: ', data);
})
.on("end", function () {
resolve("csv parse process finished");
})
.on("error", function () {
reject("csv parse process failed");
});
});
try {
await parserFcn;
} catch (error) {
console.log("Get Error: ", error);
}