Amazon S3 给出 InvalidDigest:您指​​定的 Content-MD5 无效

Amazon S3 gives InvalidDigest: The Content-MD5 you specified was invalid

这是解决方案:通过将 MD5 十六进制而不是字符串转换为 base64 来修复它。

JS 示例(Node.js 或类似的、browserify 等

new Buffer(md5, 'hex').toString('base64')

我只是花了几分钟弄清楚这个问题,所以我想我可以分享它。 :)

Content-MD5 始终是 base64 header。

The base64-encoded 128-bit MD5 digest of the message (without the headers) according to RFC 1864. This header can be used as a message integrity check to verify that the data is the same data that was originally sent. Although it is optional, we recommend using the Content-MD5 mechanism as an end-to-end integrity check. For more information about REST request authentication, go to REST Authentication in the Amazon Simple Storage Service Developer Guide Type: String Default: None Constraints: None

REST Put Object

我在从浏览器实现 blob 上传到 AWS S3 时偶然发现了这个问题。虽然选择的答案帮助我理解 Content-MD5 需要是 base64 字符串,但我没有意识到我使用的库 - md5 没有生成 base64 格式的 MD5。

我绞尽脑汁想明白为什么 MD5 无效,因为它与我手动上传到 Amazon S3 控制台时显示的 Etag 值相同,结果发现 ETag 也不是 base64。

然后我尝试了另一个名为 js-md5 的库,它有一个明确的 base64 函数来生成 base64 格式的 MD5,它通过了。

因此,如果您从 Javascript 做同样的事情,使用 js-md5 库将帮助您解决问题。