Created
November 24, 2017 12:35
-
-
Save breenie/1ab7615678f90aecce24bdabfa18028e to your computer and use it in GitHub Desktop.
SES push attachments to S3
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
| const AWS = require('aws-sdk'); | |
| const s3 = new AWS.S3(); | |
| const mailParser = require('mailparser').simpleParser; | |
| const dateFormat = require('dateformat'); | |
| const path = require('path'); | |
| const config = { | |
| incomingBucket: process.env.INCOMING_BUCKET || 'ldsc-webcam', | |
| incomingPrefix: process.env.INCOMING_PREFIX || 'incoming/', | |
| outgoingBucket: process.env.OUTGOING_BUCKET || 'ldsc-webcam', | |
| outgoingPrefix: process.env.OUTGOING_PREFIX || 'hutcam__', | |
| }; | |
| writeToBucket = (bucket, body, filename, cb) => { | |
| const params = { | |
| Body: body, | |
| Bucket: bucket, | |
| Key: filename, | |
| ServerSideEncryption: "AES256" | |
| }; | |
| s3.putObject(params, (err, data) => { | |
| if (err) { | |
| console.log(err, err.stack); | |
| } else { | |
| console.log(data); | |
| } | |
| }); | |
| }; | |
| exports.handler = function (event, context, callback) { | |
| console.log('Processing email'); | |
| const sesNotification = event.Records[0].ses; | |
| console.log("SES Notification:\n", JSON.stringify(sesNotification, null, 2)); | |
| s3.getObject({ | |
| Bucket: config.incomingBucket, | |
| Key: config.incomingPrefix + sesNotification.mail.messageId | |
| }, (err, data) => { | |
| if (err) { | |
| console.log(err, err.stack); | |
| callback(err); | |
| } else { | |
| mailParser(data.Body, (err, mail) => { | |
| const attachment = mail.attachments[0]; | |
| s3.upload( | |
| { | |
| Bucket: config.outgoingBucket, | |
| Key: config.outgoingPrefix + dateFormat(data.LastModified, 'yyyymmdd.hhMMss') + path.extname(attachment.filename), | |
| Body: attachment.content | |
| }, | |
| (err, data) => {} | |
| ); | |
| }); | |
| callback(null, null); | |
| } | |
| }); | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment