putObject makes object larger on server in Nodejs

天大地大妈咪最大 提交于 2020-01-24 14:10:09

问题


I'm using Nodejs to try and push an image to an S3 instance with the aws-sdk. Currently, it reads from a file on the client and then saves it on the server (I'm using a meteor framework.) I'd like to push it to the S3 server instead of saving it on the meteor server. When I tried to migrate it over, the images seem to gain about 30% when they are on S3. If I try and download them off of S3 the image is no longer viewable either, so it looks like it has changed encoding or something.

Here is the code to load 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);
      });
    });

回答1:


I figured out the solution, it was to encapsulate the 'file' object in a

new Buffer()

Simple, but oh so difficult to find!!



来源:https://stackoverflow.com/questions/21056525/putobject-makes-object-larger-on-server-in-nodejs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!