【发布时间】:2018-04-05 22:01:35
【问题描述】:
我可以将文件上传到 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 个字符字节)转换为 base64 表示。在nodejs中,我尝试以下,
var hexBuffer = new Buffer('ce377789add2698f68d4cb7c021e7f55', 'hex');
var base64MD5String = hexBuffer.toString('base64'); // returns 'zjd3ia3SaY9o1Mt8Ah5/VQ=='
但是,在将 base64MD5String 作为 AWS.S3.upload 中的 ContentMD5 参数传递时,我得到了错误的摘要错误。我计算/编码 MD5 的方式有什么问题?
编辑:我正在使用名为“上传任意大小的流”的亚马逊示例,但是,我已经在使用 tar'd 和 gzip'd 的文件,所以我没有管道到 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) });
【问题讨论】:
-
假设
ce377789add2698f68d4cb7c021e7f55确实是文件的正确 md5sum,那么我可以确认zjd3ia3SaY9o1Mt8Ah5/VQ==是该哈希的二进制表示的正确 base64 编码。您没有链接到您引用的特定亚马逊文档,我也找不到它。请包含该链接,并说明您是如何进行上传的——您是在编写自己的代码,还是使用 SDK?请显示一些代码?包括——你是如何构建 HTTP 请求的?您是否已捕获或转储原始 http 请求以查看其外观? -
另外有意思的是,当你上传文件时没有指定
Content-MD5:,然后在控制台中查看,ETag显示的是什么?字节数是否正确,或者太大或太小?如果您正在压缩文件并使用Content-Encoding: gzip进行上传,那么您需要获取 gzip 数据的 md5,而不是未压缩的有效负载。 -
谢谢!
ContentMD5: new Buffer(hash,'hex').toString('base64')为我工作。
标签: node.js amazon-web-services amazon-s3 character-encoding md5