1 Answer
- Newest
- Most votes
- Most comments
0
Okay, here my solution:
var s3object = (await s3.getObject({ Bucket: bucket, Key: filename }).promise());
var csvreadstream = new stream.Readable();
csvreadstream._read = () => {};
csvreadstream.push(s3object.Body);
However, the stream does not get triggered, like
.on('data')
Here the code:
const AWS = require('aws-sdk');
const utils = require('./utils');
const csv = require('fast-csv');
const stream = require('stream');
const s3 = new AWS.S3();
exports.handler = async (event) => {
console.log("Incoming Event: ", JSON.stringify(event));
const bucket = event.Records[0].s3.bucket.name;
const filename = decodeURIComponent(event.Records[0].s3.object.key.replace(/\+/g, ' '));
const message = `File is uploaded in - ${bucket} -> ${filename}`;
console.log(message);
var errors = [];
const splittedFilename = filename.split('.');
const reportFilename = splittedFilename[0] + "Report." + splittedFilename[1];
const reportBucket = 'external.transactions.reports';
var s3object = (await s3.getObject({ Bucket: bucket, Key: filename }).promise());
var csvreadstream = new stream.Readable();
csvreadstream._read = () => {};
csvreadstream.push(s3object.Body);
csvreadstream
.pipe(csv.parse({ headers: true }))
.on('data', async function(data){
this.pause();
console.log("DATA: " + data);
await utils.filterLogic(data, errors);
this.resume();
})
.on('end', async function(){
console.log("END");
await utils.writeErrorReport(errors, s3, reportBucket, reportFilename);
})
};
answered 5 years ago
Relevant content
- asked 8 months ago
- asked 4 years ago
- AWS OFFICIALUpdated a year ago
- AWS OFFICIALUpdated 4 months ago
- AWS OFFICIALUpdated 7 months ago