I am sending base64data of canvas to node.js script. I need the base64data to be stored as an image to the s3bucket. Is there any way to achieve it?
Asked
Active
Viewed 6,484 times
2 Answers
0
- 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 decoding return the string.
Pass that string to in body of S3 upload function.
var myDataUri = "data:image/jpg;base64,JVBERi0xLjMKMyAwIG9iago8PC9UeXBlIC9QYW..." var myFile=dataURItoBlob(myDataUri); function dataURItoBlob(dataURI) { var binary = atob(dataURI.split(',')[1]); var array = []; for (var i = 0; i < binary.length; i++) { array.push(binary.charCodeAt(i)); } return new Blob([new Uint8Array(array)], { type: 'image/jpg' }); } if (myFile)) { results.innerHTML = ''; var params = { Key: fileName+'.jpg', ContentType: 'image/jpg', Body: myFile }; bucket.upload(params, function(err, data) { results.innerHTML = err ? 'ERROR!' : 'UPLOADED.: ' + file; }); } else { results.innerHTML = 'Nothing to upload.'; }

Nilesh Pawar
- 655
- 7
- 12
-
@lusha li, check for new blob check link https://developer.mozilla.org/en-US/docs/Web/API/Blob/Blob – Nilesh Pawar Mar 24 '19 at 02:24
-5
you can send base64 data with AWS putObject method as follows
var AWS = require('aws-sdk');
AWS.config.loadFromPath('./s3_config.json');
var s3Bucket = new AWS.S3( { params: {Bucket: 'myBucket'} } );
var imageBase64Data='Your base64 code '
s3Bucket.putObject(imageBase64Data, function(err, data){
if (err) {
console.log(err);
console.log('Error uploading data: ', data);
} else {
console.log('succesfully uploaded the image!');
}
});

Rams
- 74
- 1
- 9