Last active
July 14, 2017 17:00
-
-
Save bkawk/1ac6def98eae60048b2b4a2104ceebfc to your computer and use it in GitHub Desktop.
Base64 Image upload to Amazon 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
'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