Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ismits/eb970789d0a8c384ffe69e1db99e3270 to your computer and use it in GitHub Desktop.
Save ismits/eb970789d0a8c384ffe69e1db99e3270 to your computer and use it in GitHub Desktop.
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!');
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment