Created
November 14, 2013 15:01
-
-
Save johnschimmel/7468267 to your computer and use it in GitHub Desktop.
possibly code for saving data uri image to 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
var filename = 'my_data_pic.png'; | |
var photoData = req.body.photoData; | |
// convert to buffer | |
var photo_buffer = new Buffer(b64str, 'base64'); | |
// prepare database record | |
var photoPost = new Photo(); // create Blog object | |
// pick the Amazon S3 Bucket | |
var s3bucket = new AWS.S3({params: {Bucket: 'dwd_uploads'}}); | |
// Set the bucket object properties | |
// Key == filename | |
// Body == contents of file | |
// ACL == Should it be public? Private? | |
// ContentType == MimeType of file ie. image/jpeg. | |
var params = { | |
Key: filename, | |
Body: photo_buffer, | |
ACL: 'public-read', | |
ContentType: mimeType | |
}; | |
// Put the Object in the Bucket | |
s3bucket.putObject(params, function(err, data) { | |
if (err) { | |
console.log(err) | |
} else { | |
console.log("Successfully uploaded data to s3 bucket"); | |
// add image to blog post | |
photoPost.image = filename; | |
} | |
photoPost.save(); | |
res.redirect('/'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can do this on front-end also by simply decoding the 64 bit data URI to original file content as follows
Store your Data URI in a variable.
Create function which decodes your data URI(64 bit encoded string) to string(Here I have created dataURItoBlob() function) and after 3 decoding return the string.
Pass that string to in body of S3 upload function.
You can send this decoded data to printer also.