AWS Lambda to index S3 new files in CloudSearch
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
exports.handler = (event, context, callback) => { | |
// WARNING : | |
// This snippet assumes : event.Records[0].eventName == 'ObjectCreated:Put' | |
// but the ful code deals with both 'ObjectCreated:Put' and 'ObjectRemoved:Delete' | |
var filename = event.Records[0].s3.object.key; | |
var bucketname = event.Records[0].s3.bucket.name; | |
var params = { | |
Bucket: bucketname, | |
Key: filename, | |
RequestPayer: 'requester', | |
}; | |
var s3 = new AWS.S3(); | |
s3.getObject(params, function (err, data) { | |
if (err) { | |
console.log('file was not found : ERROR'); | |
} | |
else { | |
var contentText = data.Body.toString('utf8'); | |
addToIndex(bucketname, filename, contentText, context); | |
} | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment