ContentMD5 Amazon S3上传Bad Digest

时间:2015-06-15 22:20:41

标签: node.js amazon-web-services amazon-s3 character-encoding md5

我可以将文件上传到S3,但是在传递ContentMD5参数时,我总是收到错误:

{ [BadDigest: The Content-MD5 you specified did not match what we received.]
  message: 'The Content-MD5 you specified did not match what we received.',
  code: 'BadDigest',
  time: Mon Jun 15 2015 17:47:19 GMT-0400 (EDT),
  statusCode: 400,
  retryable: false,
  retryDelay: 30 }

编辑:添加了链接 现在docs from amazon说:

   The output of the MD5 algorithm is a 128 bit digest.  When viewed in
   network byte order (big-endian order), this yields a sequence of 16
   octets of binary data.  These 16 octets are then encoded according to
   the base64 algorithm in order to obtain the value that is placed in
   the Content-MD5 field.  Thus, if the application of the MD5 algorithm
   over the raw data of a MIME entity results in a digest having the
   (unlikely) value of "Check Integrity!", then that MIME entity's
   header could contain the field

所以看起来有一个文件返回这个md5,

computer:NU isaac$ md5 ~/Desktop/mediumFile.dat 
MD5 (/Users/isaac/Desktop/mediumFile.dat) = ce377789add2698f68d4cb7c021e7f55

我必须将十六进制表示(2个char字节)转换为base64表示。在nodejs中,我尝试以下内容,

var hexBuffer = new Buffer('ce377789add2698f68d4cb7c021e7f55', 'hex');
var base64MD5String = hexBuffer.toString('base64'); // returns 'zjd3ia3SaY9o1Mt8Ah5/VQ=='

但是,当我将base64MD5String作为AWS.S3.upload中的ContentMD5参数传递时,我收到了错误的摘要错误。我计算/编码MD5的方式有什么问题?

编辑:我正在使用标题为'上传任意大小的流的亚马逊示例'但是,我已经在处理tar&tar;和gzip的文件了,所以我没有使用zlib。

var fs = require('fs');

var body = fs.createReadStream('bigfile');
var s3obj = new AWS.S3({params: {Bucket: 'myBucket', Key: 'myKey'}});
s3obj.upload({Body: body}).
  on('httpUploadProgress', function(evt) { console.log(evt); }).
  send(function(err, data) { console.log(err, data) });

1 个答案:

答案 0 :(得分:0)

在我所做的之下,它运作良好。

var md5 = require('md5');
var hash = md5('test');

ContentMD5: new Buffer(hash,'hex').toString('base64');