Skip to content

Instantly share code, notes, and snippets.

@mzaradzki
Created May 29, 2017 09:13
Show Gist options
  • Save mzaradzki/0a940b1351bf3ac3578253e90b1390f9 to your computer and use it in GitHub Desktop.
Save mzaradzki/0a940b1351bf3ac3578253e90b1390f9 to your computer and use it in GitHub Desktop.
AWS Lambda to index S3 new files in CloudSearch
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