Last active
February 7, 2019 17:47
-
-
Save uhlenbrock/67ab81e704f7a2175d3a51020d050cdf to your computer and use it in GitHub Desktop.
Use Lambda & S3 to save a copy of all sent email via Braze
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
Webhook URL: https://<api_gateway_url>/prod/uploadEmail?user_id={{${user_id}}}&template_name=<template_name> | |
Request Body: (select raw text), paste in entire email HTML |
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
'use strict'; | |
const uploadService = require('./service'); | |
exports.handler = (event, context, callback) => { | |
const filename = uploadService.buildFilename(event.queryStringParameters); | |
uploadService.saveEmailToS3(event.body, filename); | |
const response = { | |
statusCode: 200, | |
body: JSON.stringify({ "message": 'Email ' + filename + ' saved!'}) | |
}; | |
callback(null, response); | |
}; |
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
'use strict'; | |
const AWS = require('aws-sdk'); | |
function buildFilename(params) { | |
const now = Date.now(); | |
return params.user_id + "/" + now + "_" + params.template_name + ".html"; | |
} | |
function saveEmailToS3(data, filename) { | |
AWS.config.update({ accessKeyId: process.env.ACCESS_KEY, secretAccessKey: process.env.SECRET_KEY }); | |
const s3 = new AWS.S3(); | |
console.log("Start uploading file to S3"); | |
const param = { Bucket: env.BUCKET, Key: filename, Body: data }; | |
s3.putObject(param, function(err, data) { | |
if (err) { | |
console.log(err, err.stack); | |
} else { | |
console.log('Successfully uploaded data'); | |
} | |
}); | |
} | |
module.exports = { | |
saveEmailToS3: saveEmailToS3, | |
buildFilename: buildFilename | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment