I want to read a file line by line located on S3. I tried the following code which I found searching online, but the Lambda function is exiting without invoking any of the readline callbacks. What am I doing wrong?
const aws = require('aws-sdk');
const s3 = new aws.S3({ apiVersion: '2006-03-01' });
const readline = require('readline');
exports.handler = async (event, context, callback) => {
const bucket = event.Records[0].s3.bucket.name;
const key = decodeURIComponent(event.Records[0].s3.object.key.replace(/\+/g, ' '));
const params = {
Bucket: bucket,
Key: key,
};
const s3ReadStream = s3.getObject(params).createReadStream();
const rl = readline.createInterface({
input: s3ReadStream,
terminal: false
});
rl.on('line', (line) => {
console.log(`Line from file: ${line}`);
});
rl.on('error', () => {
console.log('error');
});
rl.on('close', function () {
console.log('closed');
context.succeed();
});
console.log('done');
};
I've found the problem. It's being awhile that I haven't coded on Lambda and I thought it would only exit when context was called. I'm now waiting for the promise to be resolved (or rejected which I'll implement later).
const aws = require('aws-sdk');
const s3 = new aws.S3({ apiVersion: '2006-03-01' });
const readline = require('readline');
exports.handler = async (event, context, callback) => {
const bucket = event.Records[0].s3.bucket.name;
const key = decodeURIComponent(event.Records[0].s3.object.key.replace(/\+/g, ' '));
const params = {
Bucket: bucket,
Key: key,
};
const s3ReadStream = s3.getObject(params).createReadStream();
const rl = readline.createInterface({
input: s3ReadStream,
terminal: false
});
let myReadPromise = new Promise((resolve, reject) => {
rl.on('line', (line) => {
console.log(`Line from file: ${line}`);
});
rl.on('error', () => {
console.log('error');
});
rl.on('close', function () {
console.log('closed');
resolve();
});
});
try { await myReadPromise; }
catch(err) {
console.log('an error has occurred');
}
console.log('done reading!');
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With