1

I'm trying to upload a file to S3 via a pre-signed URL. I've verified that the URL indeed works by testing it with curl

curl --request PUT --upload file {filename} "{url}"

It pushes it up there A-OK.

However, when trying it from javascript, I get this message:

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

I've taken as many debugging steps as I can come up with -- for instance, making sure that the content-types and Content Lengths match between the pre-signed url and what I actually try to upload.

I found this SO Thread And tried everything in there:

  • My Keys have no trailing spaces or slashes
  • My bucketname doesn't have a slash in it.
  • Tried URL encoding my key -- no difference
  • Made sure my keyname is compliant

So, I'm at a bit of a loss. Could anyone identify what would be causing S3 to reject this request?

Javascript:

$(document).ready(function () {
            $('[type=submit]').click(function (evt) {
                evt.preventDefault();
                
                console.log($('#id_attachment').get(0).files[0].size);

                var reader = new FileReader();
                reader.onloadend = function (evt) {
                    console.log(evt.target.result);
                    $.ajax({
                        url: 'https://bucketname.s3.amazonaws.com/simple.png?Signature=vYIEOmAay9v6zwB1cz78FhXv6Yo%3D&Expires=1416243285&AWSAccessKeyId=ACCESSKEY',
                        type: 'PUT',
                        contentType: "image/png",
                        data: evt.target.result,
                        success: function () {
                            console.log('Uploaded data successfully.');
                        }
                    });
                };
                reader.readAsBinaryString($('#id_attachment').get(0).files[0]);

            });


        });
Community
  • 1
  • 1
user3308774
  • 1,354
  • 4
  • 16
  • 20
  • Another possible cause for this is when your computer local time has a noticeable offset from amazon's. Above one minute offset triggers this error for me. – ffflabs Nov 17 '14 at 17:06
  • The error message is different in that case. – TJ- Nov 17 '14 at 17:57

1 Answers1

2

When creating the pre-signed URL, are you specifying a Content-Type of image/png, or any Content-Type at all?

If you aren't, because you are including a Content-Type in the browser upload, the signature mismatch Amazon might be referring to is that the ajax PUT does have a Content-Type header, but signature on the signed URL does not.

I recently ran into this issue because I'm creating pre-signed URLs with the .NET AWSSDK, and not specifying a content type at the time of the URL generation.

Uploads work fine from some run-of-the-mill sample C# code, but failed in my browser app because the framework I was using automatically appended the Content-Type header, causing the mismatch. Re-playing the request with a web debbugger (I'm using Fiddler), and removing the Content-Type header from the browser's request worked, and is what led me to figuring out what was causing the mismatch for me.

Kaz
  • 143
  • 1
  • 9