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
How to Remove Non-Alphanumeric Characters
How to Use Guzzle to Send a Post Request in Json
Is There a Built-In Way to Get All of the Changed/Updated Fields in a Doctrine 2 Entity
How Does the Keyword "Use" Work in PHP and How to Import Classes With It
Difference Between MySQL, MySQLi and Pdo
How to Check If an Array Value Exists
Pdo::Fetchall Vs. Pdo::Fetch in a Loop
How to Pass Variable Number of Arguments to a PHP Function
Generating (Pseudo)Random Alpha-numeric Strings
MySQL Check If a Table Exists Without Throwing an Exception
How to Change Public Folder to Public_Html in Laravel 5
How to Check Whether the User Uploaded a File in PHP
How to Replace Different Newline Styles in PHP the Smartest Way
How to Prevent Multiple Form Submission on Multiple Clicks in PHP