Created
February 28, 2018 06:29
-
-
Save isaaguilar/2e8b4c9f1cebfaa8883a70d497331f03 to your computer and use it in GitHub Desktop.
Listen to S3 event, read the file, and publish to an SNS topic
This file contains hidden or 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
| var AWS = require('aws-sdk'); | |
| var s3 = new AWS.S3(); | |
| var sns = new AWS.SNS(); | |
| exports.handler = (event, context, callback) => { | |
| var bucketName = process.env.bucketName; | |
| var keyName = event.Records[0].s3.object.key; | |
| readFile(bucketName, keyName, readFileContent, onError); | |
| }; | |
| function readFile (bucketName, filename, readFileContent, onError) { | |
| var params = { | |
| Bucket: bucketName, | |
| Key: filename | |
| }; | |
| // | |
| s3.getObject(params, function (err, data) { | |
| if (!err) readFileContent(filename, data.Body.toString()); | |
| else console.log(err); | |
| }); | |
| } | |
| function readFileContent(filename, content) { | |
| sns.publish({ | |
| Message: content, | |
| TopicArn: process.env.topicArn | |
| }, function(err, data) { | |
| if (err) { | |
| console.log(err.stack); | |
| return; | |
| } | |
| console.log('push sent'); | |
| // context.done(null, 'Function Finished!'); | |
| }); | |
| } | |
| function onError (err) { | |
| console.log('error: ' + err); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment