Skip to content

Instantly share code, notes, and snippets.

@maephisto
Created June 16, 2014 11:30
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save maephisto/115d4237ada17aef5b8a to your computer and use it in GitHub Desktop.
Save maephisto/115d4237ada17aef5b8a to your computer and use it in GitHub Desktop.
Node upload base64 image data to AWS
var config = {
"aws" : {
"bucket": "...",
"domain" : "...",
"prefix" : "..",
"path": "my/test/",
"credentials" : {
"accessKeyId": "...",
"secretAccessKey": "..."
}
}
};
var AWS = require('aws-sdk');
module.exports.storeImage = function(base64EncodedImage, config, callback) {
var buf = new Buffer(base64EncodedImage, 'base64');
AWS.config = config.aws.credentials;
var key = getUniqueFilename(config);
var s3 = new AWS.S3();
s3.putObject({
Bucket: config.aws.bucket,
Key: key,
Body: buf,
ACL: 'public-read',
ContentType: "image/png",
ContentEncoding: "base64"
}, function(error, data) {
callback(data, error, key);
});
};
var getUniqueFilename = function(config) {
var timestamp = (new Date()).getTime();
var randomInteger = Math.floor((Math.random() * 1000000) + 1);
return config.aws.path + timestamp + '_' + randomInteger + '.png';
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment