Skip to content

Instantly share code, notes, and snippets.

@breenie
Created November 24, 2017 12:35
Show Gist options
  • Select an option

  • Save breenie/1ab7615678f90aecce24bdabfa18028e to your computer and use it in GitHub Desktop.

Select an option

Save breenie/1ab7615678f90aecce24bdabfa18028e to your computer and use it in GitHub Desktop.
SES push attachments to S3
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