Created
August 27, 2021 16:18
-
-
Save ismits/eb970789d0a8c384ffe69e1db99e3270 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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