PutObject makes an object larger on the server in Nodejs

I am using Nodejs to try to push an image onto an S3 instance using aws-sdk. It currently reads from a file on the client and then saves it on the server (I use the meteor structure.) I would like to push it to the S3 server instead of saving the meteorites to the server. When I tried to transfer it, the images seem to get about 30% when they are on S3. If I try to download them from S3, the image will no longer be visible, so it looks like it changed the encoding or something like that.

Here is the code to download the file on the client side:

saveFile = function( blob, name, path, type, callback ) { var fileReader = new FileReader(); var method; var encoding = 'binary'; var type = type || 'binary'; switch( type ) { case 'text': method = 'readAsText'; encoding = 'utf8'; break; case 'binary': method = 'readAsBinaryString'; encoding = 'binary'; break; default: method = 'readAsBinaryString'; encoding = 'binary'; break; } // Call the save function on the server after the file has been read. fileReader.onload = function( file ) { console.log( "File loaded..." ); Meteor.call( 'saveFile', file.srcElement.result, name, path, encoding, callback ); } // Read the file fileReader[ method ]( blob ); } 

On the server side:

  saveFile: function( file, name, path, encoding ) { s3.createBucket({Bucket: bucketName}, function() { var params = {Bucket: bucketName, Key: keyName, ContentType: 'binary', ContentEncoding: 'utf8', Body: file}; s3.putObject(params, function(err, data) { if (err) console.log(err) else console.log("Successfully uploaded data to " + bucketName + "/" + keyName); }); }); 
+2
source share
1 answer

I understood the solution, it was to encapsulate the file object in

 new Buffer() 

Simple but oh so hard to find !!

+2
source

Source: https://habr.com/ru/post/973883/


All Articles