Technology
-
Amazon Lambda – E-mail via SNS the URL of an S3 file when created
I threw together the following Lambda function to send an e-mail via SNS (Amazon’s Simple Notification Service) whenever a file is created in S3 (Amazon’s Simple Storage Service.) Yes, it’s barely commented and overly loggy, but you get the idea: 'use strict'; console.log('Loading function'); const aws = require('aws-sdk'); const s3 = new aws.S3({ apiVersion: '2006-03-01' }); exports.handler = (event, context, callback) => { //console.log('Received event:', JSON.stringify(event, null, 2)); // Get the object from the event and show its content type const bucket = event.Records[0].s3.bucket.name; const key = decodeURIComponent(event.Records[0].s3.object.key.replace(/\+/g, ' ')); const params = { Bucket: bucket, Key: key, }; s3.getObject(params, (err, data) => { if (err) { console.log(err); const…