The AWS Ruby SDK gives me a "The Content-MD5 you specified was invalid." error when I try to upload using a presigned url

StackOverflow https://stackoverflow.com/questions/23417194

Question

I'm having problems using the AWS Ruby SDK to create a pre-signed upload link to S3 while specifying an md5 hash.

md5 -q my_file.jpg
4ef248082efb309d50f1cbbbd3d7cf4a

From Rails Console:

AWS::S3.new.buckets[bucket].objects[key].url_for(:put, signature_version: :v4, content_md5: '4ef248082efb309d50f1cbbbd3d7cf4a').to_s
=> "https://s3.amazonaws.com/<rest_of_url_here>&X-Amz-SignedHeaders=Host%3BContent-MD5" 

From command line:

curl -v -H "content-md5:4ef248082efb309d50f1cbbbd3d7cf4a" -T my_file.jpg "https://s3.amazonaws.com/<rest_of_url_here>&X-Amz-SignedHeaders=Host%3BContent-MD5"

<Error><Code>InvalidDigest</Code><Message>The Content-MD5 you specified was invalid.</Message>    <RequestId>blah</RequestId><Content-MD5>4ef248082efb309d50f1cbbbd3d7cf4a</Content-MD5><HostId>blah</HostId></Error>

I can generate an upload link and upload without using content-md5. After doing that upload, I verified that the S3 object does indeed have the same MD5 hash as my local file.

Was it helpful?

Solution

Apparently you need the MD5 base64 encoded. Note that it's not the MD5 hex string, but the actual MD5 that needs to be encoded. I should have been using the following value:

openssl dgst -md5 -binary my_file.jpg | openssl enc -base64
TvJICC77MJ1Q8cu709fPSg==
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top