Amazon S3 - How to Fix 'The Request Signature We Calculated Does Not Match the Signature' Error

Amazon S3 - How to fix 'The request signature we calculated does not match the signature' error?

After two days of debugging, I finally discovered the problem...

The key I was assigning to the object started with a period i.e. ..\images\ABC.jpg, and this caused the error to occur.

I wish the API provides more meaningful and relevant error message, alas, I hope this will help someone else out there!

Amazon S3 Error: The request signature we calculated does not match the signature you provided. Check your key and signing method

This is an interesting one, and basically it boils down to the Android implementation of HttpURLConnection:

com.squareup.okhttp.internal.huc.HttpURLConnectionImpl

behaving differently to the JVM provided implementation:

sun.net.www.protocol.http.HttpURLConnection

The relevant difference here, is that around line 323 the Android implementation checks both:

  • whether the doOutput flag has been set, and
  • whether the method field is "GET"

If both these are true, it "helpfully" changes the method to "POST", which means the signature, based on the canonical request which includes the request method, is no longer valid. This is why the other samples work without issue; as they are already using a request method besides "GET", it isn't changed.

In the AWS sample code, the doOutput field is set at:

com/amazonaws/services/s3/sample/util/HttpUtils.java:86

so what I would suggest going forward is to use the definition of:

com.amazonaws.services.s3.sample.GetS3ObjectSample.getS3Object(...)

as a guide to how to calculate the required Authorization header, but use your preferred sane HTTP client instead of the:

com.amazonaws.services.s3.sample.util.HttpUtils

class provided with the samples.

Amazon S3 - How to fix 'The request signature we calculated does not match the signature' error?

After two days of debugging, I finally discovered the problem...

The key I was assigning to the object started with a period i.e. ..\images\ABC.jpg, and this caused the error to occur.

I wish the API provides more meaningful and relevant error message, alas, I hope this will help someone else out there!



Related Topics



Leave a reply



Submit