Created
May 25, 2017 20:57
-
-
Save audy/3fcfa0d5dfdba3f48ad7591b13ca92b8 to your computer and use it in GitHub Desktop.
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
// post a message to Slack when a file is added to an S3 bucket | |
'use strict'; | |
console.log('Loading function'); | |
const aws = require('aws-sdk'); | |
const https = require('https'); | |
const s3 = new aws.S3({ apiVersion: '2006-03-01' }); | |
exports.handler = (event, context, callback) => { | |
const key = decodeURIComponent(event.Records[0].s3.object.key.replace(/\+/g, ' ')); | |
console.log('new S3 object!', key) | |
const options = { | |
hostname: 'hooks.slack.com', | |
path: '<CUSTOM SLACK URL GOES HERE>', | |
method: 'POST' | |
} | |
const payload = { | |
"text": `New file ${key}`, | |
"username": 's3-notifier', | |
"icon_url": '' | |
} | |
const req = https.request(options, function (res,b , c) { | |
res.setEncoding('utf8'); | |
res.on('data', function (chunk) { | |
}); | |
}); | |
req.on('error',function(e) { | |
console.log('problem with request: ' + e.message); | |
}); | |
req.write(JSON.stringify(payload)); | |
req.end(); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment