putObject使对象在服务器上更大的Nodejs更大、器上、对象、putObject

2023-09-11 10:00:00 作者:一切从零开始

我用Nodejs,试图推动一个形象的S3实例的AWS-SDK。目前,它读取客户机上的文件,然后在服务器(我使用的是一颗流星框架。)我想推它保存流星服务器上的S3服务器上,而不是将其保存。当我试图迁移过来,图像似乎获得约30%的时候都在S3上。如果我尝试下载它们赶走S3的形象不再是可见要么,所以看起来它已经改变编码什么的。

下面是code加载在客户端文件:

  saveFile的=功能(BLOB,名称,路径,类型,回调){
  VAR的FileReader =新的FileReader();
  VaR方法;
  VAR编码='二进制';
  VAR type =类型|| 二进制;

  开关(类型){
    案文本:
      方法='readAsText';
      编码='UTF8';
      打破;
    案二进制:
      方法='readAsBinaryString';
      编码='二进制';
      打破;
    默认:
      方法='readAsBinaryString';
      编码='二进制';
      打破;
  }

  //调用保存功能在服务器上的文件已被读取之后。
  fileReader.onload =功能(文件){
    的console.log(文件加载...);
    Meteor.call('了saveFile',file.srcElement.result,名称,路径,编码,回调);
  }

  //读取文件
  的FileReader【制法】(BLOB);
}
 

在服务器端:

  saveFile的:功能(文件名,路径,编码){
    s3.createBucket({斗:bucketName},函数(){
      VAR PARAMS = {斗:bucketName,重点:的keyName,则contentType:二进制,ContentEncoding:UTF8,正文:文件};
      s3.putObject(PARAMS,功能(ERR,数据){
        如果(ERR)
          执行console.log(ERR)
        其他
          的console.log(成功上传数据+ bucketName +/+注册表项目);
      });
    });
 
直播平台建设为什么要用云存储,怎么用,有什么好处

解决方案

我想出了解决办法,这是封装在'文件'对象

 新的缓冲区()
 

简单,但噢所以很难找到!

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

解决方案

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

new Buffer()

Simple, but oh so difficult to find!!

 
精彩推荐