最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin0浏览0评论

This is the solution: fix it by converting the MD5 hex to base64 instead of the string.

Example for JS (Node.js or similar, browserify, etc.

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

I just spent few minutes to figure this out so I thought I could share it. :)

This is the solution: fix it by converting the MD5 hex to base64 instead of the string.

Example for JS (Node.js or similar, browserify, etc.

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

I just spent few minutes to figure this out so I thought I could share it. :)

Share Improve this question asked Nov 7, 2015 at 0:19 alsdkjasdlkjaalsdkjasdlkja 1,2983 gold badges15 silver badges30 bronze badges 2
  • 2 Your "question" is an answer, but I found it useful nevertheless :) – scorpiodawg Commented Nov 10, 2017 at 1:19
  • Thanks for sharing your answer here! Anyways, could you please answer your own question below? I think it would be more clear when doing that :-) – Uvuvwevwevwe Commented Oct 22, 2020 at 9:24
Add a comment  | 

3 Answers 3

Reset to default 10

Content-MD5 is always a 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

I stumbled upon this question when implementing blob upload to AWS S3 from the browser. While the chosen answer helped me understand that the Content-MD5 needs to be a base64 string, I didn't realize that the library I used - md5 was not producing MD5 in the base64 format.

I scratched my head to understand why the MD5 was invalid because it was the same value as the Etag shown on the Amazon S3 console when I manually upload it there and it turns out the ETag was also not base64.

Then I tried another library called js-md5 that has an explicit base64 function to produce MD5 in the base64 format and it went through.

So if you are doing the same thing from Javascript, using the js-md5 library will help you solve your problem.

For anyone looking for code examples or alternatives, here are three different solutions using built-in and external libs.

NodeJS built-in:

const crypto = require("crypto");
const hash = crypto.createHash("md5").update(content).digest("base64");

Using md5 lib:

const md5 = require("md5");
const hash = Buffer.from(md5(content), "hex").toString("base64");

or

const md5 = require("md5");
const md5Bytes = md5(content, {
  encoding: "binary",
  asBytes: true,
});
const hash = Buffer.from(md5Bytes).toString("base64");

The content variable may be a string or a typed array (e.g.: Buffer, Uint8Array, Int32Array...).

发布评论

评论列表(0)

  1. 暂无评论