将二进制数据发送到Amazon

将二进制数据发送到Amazon

本文介绍了将二进制数据发送到Amazon S3(Javascript)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Amazon S3 interprets my binary data as non-UTF-8 and modifies it when I write to a bucket.

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

Example using the official s3 Javascript client:

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

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

Returns 105 while the original png_file is 85. S3 somehow modifies my file, and I think it has to do with charsets.

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

If I uncomment the Content-Length line, I get a 400 error on putObject(): The Content-MD5 you specified did not match what we received.

如果我用ContentMD5: crypto.createHash("md5").update(png_file).digest("base64")自己计算MD5哈希(而不是让S3库执行),则得到相同的结果.这似乎确认了我发送的数据和S3接收的数据之间的差异.

I get the same result if I calculate the MD5 hash myself (instead of letting the S3 library do it) with ContentMD5: crypto.createHash("md5").update(png_file).digest("base64"). This seems to acknowledge a difference between the data I send and the one S3 receives.

我已阅读标题相似的问题 ,但并不能解决问题.

I have read a similarly titled issue, but it didn't solve the problem.

推荐答案

S3 putObject()假定为Buffer或UTF-8字符串.我应该以二进制形式发送二进制文件,而不是以二进制字符串"的形式发送,这意味着使用new Buffer(...)而不是new Buffer(...).toString("binary").

S3 putObject() assumes either a Buffer or an UTF-8 string. I should have sent the binary as it, not as a "binary string", meaning using new Buffer(...) instead of new Buffer(...).toString("binary").

这篇关于将二进制数据发送到Amazon S3(Javascript)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 20:21