Skip to content

Instantly share code, notes, and snippets.

@bkawk
Last active July 14, 2017 17:00
Show Gist options
  • Save bkawk/1ac6def98eae60048b2b4a2104ceebfc to your computer and use it in GitHub Desktop.
Save bkawk/1ac6def98eae60048b2b4a2104ceebfc to your computer and use it in GitHub Desktop.
Base64 Image upload to Amazon S3
'use strict';
const aws = require('aws-sdk');
exports.s3PutBase64 = (image, folder, imageName) => {
return new Promise(function(resolve, reject) {
const buf = new Buffer(image.replace(/^data:image\/\w+;base64,/, ""), 'base64');
const s3 = new aws.S3();
aws.config = {
"accessKeyId": process.env.AWS_ACCESS_KEY_ID,
"secretAccessKey": process.env.AWS_SECRET_ACCESS_KEY
};
s3.putObject({
Bucket: process.env.AWS_S3_BUCKET,
Key: folder + '/' + imageName,
Body: buf,
ACL: 'public-read',
ContentType: "image/png",
ContentEncoding: "base64"
}, (error, data) => {
if (error){
reject(error);
} else {
resolve(data);
}
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment