node.js - putObject 使 Nodejs 中的服务器上的对象变大

标签 node.js amazon-web-services amazon-s3 meteor

我正在使用 Nodejs 尝试使用 aws-sdk 将图像推送到 S3 实例。目前,它从客户端上的文件读取,然后将其保存在服务器上(我使用的是meteor框架。)我想将其推送到S3服务器,而不是将其保存在meteor服务器上。当我尝试将其迁移过来时,图像在 S3 上时似乎增加了约 30%。如果我尝试从 S3 下载它们,图像也不再可见,因此看起来它已经更改了编码或其他内容。

以下是在客户端加载文件的代码:

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 );
}

在服务器端:

  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);
      });
    });

最佳答案

我找到了解决方案,即将"file"对象封装在

new Buffer()

很简单,但是很难找到!!

关于node.js - putObject 使 Nodejs 中的服务器上的对象变大,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21056525/

相关文章:

amazon-web-services - AWS Athena 创建表和分区

amazon-web-services - 我可以使用 CloudFormation 模板更新 AWS Lambda 函数吗?

node.js - npm 不会安装包 "npm ERR! network tunneling socket could not be established, cause=Parse Error"

mysql - 如何在 node.js 中使用带有 mysql 连接和池的单例设计模式

amazon-web-services - 如何使用 Cloudformation 将 lambda 函数附加到现有 API 网关

postgresql - 如何更改 AWS RDS Postgres 数据库时区?

amazon-web-services - 具有 Lambda 集成的 Terraform API 网关

ios - 带有AFAmazonS3Manager + AFNetworking的Amazon s3

node.js - Sail.js 运行命令刷新数据库后需要重新启动服务器

javascript - 在哪里可以找到 AWS-SDK 中用于 NodeJS typescript 中的 dynamodb 的所有异常类?