Skip to content

Ajax put method upload file to s3 SignatureDoesNotMatch bug solve #207

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from

Conversation

vkill
Copy link

@vkill vkill commented Mar 25, 2013

I want use ajax upload file to s3 with put method.

First, I generate s3 object write url use aws-sdk

require 'aws-sdk'
AWS.config(
  access_key_id: ENV['AWS_ACCESS_KEY_ID'],
  secret_access_key: ENV['AWS_SECRET_ACCESS_KEY']
)
s3 = AWS::S3.new
bucket = s3.buckets[ENV['AWS_S3_BUCKET']]
obj = bucket.objects['tmp/file']
url = obj.url_for :write, expires: 3600
url.to_s

Second, i use ajax upload, use default options

the upload html: https://github.com/vkill/browser_upload_file_to_s3_demo/blob/master/public/upload_test.html

$.ajax({
    type: 'put',
    url: url,
    dataType: 'xml',
    processData: false,
    data: file
});

the result like this

OPTIONS https://dev-playab-net.s3.amazonaws.com/tmp/file?AWSAccessKeyId=AKIAI6GLQRTI5FAZMNRA&Expires=1364064748&Signature=7TYtdwb%2BSVK%2FTwPIok%2FCuvWbYTw%3D 200 (OK)
PUT https://dev-playab-net.s3.amazonaws.com/tmp/file?AWSAccessKeyId=AKIAI6GLQRTI5FAZMNRA&Expires=1364064748&Signature=7TYtdwb%2BSVK%2FTwPIok%2FCuvWbYTw%3D 403 (Forbidden)

<?xml version="1.0" encoding="UTF-8"?>
<Error><Code>SignatureDoesNotMatch</Code><Message>The request signature we calculated does not match the signature you provided. Check your key and signing method.</Message><StringToSignBytes>50 55 54 0a 0a 61 70 70 6c 69 63 61 74 69 6f 6e 2f 78 2d 77 77 77 2d 66 6f 72 6d 2d 75 72 6c 65 6e 63 6f 64 65 64 3b 20 63 68 61 72 73 65 74 3d 55 54 46 2d 38 0a 31 33 36 34 30 36 34 37 34 38 0a 2f 64 65 76 2d 70 6c 61 79 61 62 2d 6e 65 74 2f 74 6d 70 2f 66 69 6c 65</StringToSignBytes><RequestId>A2659C0EAF2E0BDD</RequestId><HostId>hGIUqnKQDXVpRyl98OdSjWTlrIdQF2HVFITulrLby4WTuSiq7NeDtRiUudjkhVOh</HostId><SignatureProvided>7TYtdwb+SVK/TwPIok/CuvWbYTw=</SignatureProvided><StringToSign>PUT

application/x-www-form-urlencoded; charset=UTF-8
1364064748
/dev-playab-net/tmp/file</StringToSign><AWSAccessKeyId>AKIAI6GLQRTI5FAZMNRA</AWSAccessKeyId></Error> 

Third, i set Content-Type header and use ajax upload

$.ajax({
    type: 'put',
    url: url,
    dataType: 'xml',
    processData: false,
    data: file,
    headers: {
      'Content-Type': file.type
    }
});

the result like this

OPTIONS https://dev-playab-net.s3.amazonaws.com/tmp/file?AWSAccessKeyId=AKIAI6GLQRTI5FAZMNRA&Expires=1364064748&Signature=7TYtdwb%2BSVK%2FTwPIok%2FCuvWbYTw%3D 200 (OK)

PUT https://dev-playab-net.s3.amazonaws.com/tmp/file?AWSAccessKeyId=AKIAI6GLQRTI5FAZMNRA&Expires=1364064748&Signature=7TYtdwb%2BSVK%2FTwPIok%2FCuvWbYTw%3D 403 (Forbidden)

<?xml version="1.0" encoding="UTF-8"?>
<Error><Code>SignatureDoesNotMatch</Code><Message>The request signature we calculated does not match the signature you provided. Check your key and signing method.</Message><StringToSignBytes>50 55 54 0a 0a 61 70 70 6c 69 63 61 74 69 6f 6e 2f 78 2d 72 75 62 79 0a 31 33 36 34 30 36 34 37 34 38 0a 2f 64 65 76 2d 70 6c 61 79 61 62 2d 6e 65 74 2f 74 6d 70 2f 66 69 6c 65</StringToSignBytes><RequestId>5C3072B338FDF1BE</RequestId><HostId>ZMaN/ccYiRE61ZOzbApu8iZaFyO/YTyGG7LfZDAtKZGNv3oX0S9dAOUBj/uHjR7p</HostId><SignatureProvided>7TYtdwb+SVK/TwPIok/CuvWbYTw=</SignatureProvided><StringToSign>PUT

application/x-ruby
1364064748
/dev-playab-net/tmp/file</StringToSign><AWSAccessKeyId>AKIAI6GLQRTI5FAZMNRA</AWSAccessKeyId></Error> 

So, i view source, https://github.com/aws/aws-sdk-ruby/blob/master/lib/aws/s3/s3_object.rb#L1340 and https://github.com/aws/aws-sdk-ruby/blob/master/lib/aws/s3/request.rb#L93 , discover s3_object#string_to_sign error, missing 'content-md5' header and 'content-type' header.

I already solved it, please merge.

Thanks.

@vkill vkill closed this May 11, 2013
trevorrowe added a commit that referenced this pull request May 13, 2013
…url_for.

The S3 signature requries the content-md5 and content-type headers to be
signed.  The #url_for helper method previously did not accept these two
values and therefore was only good for requests where these values
were not specified.

You can now specify them like so:

    s3.buckets['bucket-name'].objects['key'].url_for(:write, {
      # both of these are optional
      :content_type => 'text/plain',
      :content_md5 => '...',
    })

Fixes #207
trevorrowe added a commit that referenced this pull request Feb 17, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant