将二进制数据发送到 Amazon S3 (Javascript)

ehm*_*cky 3 javascript binary amazon-s3 amazon-web-services

Amazon S3 将我的二进制数据解释为非 UTF-8,并在我写入存储桶时对其进行修改。

使用官方 s3 Javascript 客户端的示例:

var png_file = new Buffer( "iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==", "base64" ).toString( "binary" );
s3.putObject( { 
  Bucket: bucket, 
  Key: prefix + file, 
  ContentType: "image/png;charset=utf-8", 
  CacheControl: "public, max-age=31536000",
  Body: png_file 
  // , ContentLength: png_file.length
}, function( e ){
  if ( e ) {
    console.log( e );
  } else {
    s3.getObject( { 
      Bucket: bucket, 
      Key: prefix + file
    }, function( e, v ) {
      if ( e ) {
        console.log( e )
      } else {
        console.log( v.ContentLength );
      }
    } );
  }
} );
Run Code Online (Sandbox Code Playgroud)

返回105时原件png_file85. S3 以某种方式修改了我的文件,我认为这与字符集有关。

如果我取消注释该行,则会在:Content-Length上收到 400 错误。putObject()The Content-MD5 you specified did not match what we received

如果我自己计算 MD5 哈希值(而不是让 S3 库来计算),我会得到相同的结果ContentMD5: crypto.createHash("md5").update(png_file).digest("base64")。这似乎承认我发送的数据和 S3 接收的数据之间存在差异。

我读过一个类似标题的问题,但它没有解决问题。

ehm*_*cky 5

S3putObject()假定一个 Buffer 或一个 UTF-8 字符串。我应该按原样发送二进制文件,而不是作为“二进制字符串”,这意味着使用new Buffer(...)而不是new Buffer(...).toString("binary").