-
-
Save chris-ramon/0cc8994966d513d4719d1b88114d21b8 to your computer and use it in GitHub Desktop.
Example Lambda Function to process lines of text files when uploaded to S3
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
'use strict'; | |
var AWS = require('aws-sdk'); | |
var S3 = new AWS.S3(); | |
var readline = require('readline'); | |
exports.handler = function (event, context) { | |
//Get S3 file bucket and name | |
//Make sure to loop through event.Records, don't assume there is only 1 in production!!! | |
var bucket = event.Records[0].s3.bucket.name; | |
var key = event.Records[0].s3.object.key; | |
//Create read stream from S3 | |
var s3ReadStream = S3.getObject({Bucket: bucket, Key: key}).createReadStream(); | |
//handle stream errors. | |
//Pass the S3 read stream into the readline interface to break into lines | |
var readlineStream = readline.createInterface({input: s3ReadStream, terminal: false}); | |
//handle stream errors | |
var totalLineCount = 0; | |
var totalCharCount = 0; | |
readlineStream.on('line', function (line) { | |
//Do whatever you need with the line | |
//In this example we are just counting the number of lines and characters | |
totalLineCount += 1; | |
totalCharCount += line.length; | |
}); | |
readlineStream.on('close', function () { | |
//Do cleanup here such as persist resultant calculations. | |
//In this example I'll just print to the log the total number of lines: | |
console.log("In s3://" + bucket + "/" + key + " there are " + totalLineCount + " lines, " + totalCharCount + " chars."); | |
context.succeed(); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment