5

Just started working with the node.js aws client to generate a presigned url and send it to the browser for the user to upload the file, but I get the following message:

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

I have references quite a few links and it seems really basic, but I seem to be failing

https://github.com/aws/aws-sdk-js/issues/251

Direct Browser Upload to S3 with Meteor, jQuery and the AWS SDK

https://forums.aws.amazon.com/thread.jspa?messageID=556839

Either, I am completely stupid or the sdk is really difficult to use

node:

var putParams = {
      Bucket: config.aws.s3UploadBucket,
      Key: filename,
      ACL: 'public-read',
      Expires: 120,
      Body: '',
      ContentMD5: 'false'
    };
s3.getSignedUrl('putObject', putParams, function (err, url) {
  if (!!err) {
    console.error(err);
    res.json({error: ''});
    return;
  }

  res.json({
    'awsAccessKeyId': config.aws.accessKeyId,
    's3bucket': config.aws.s3UploadBucket,
    's3key': filename,
    's3policy': s3policy.policy,
    's3signature': s3policy.signature,
    'url': url
  });
});

client:

  var fd = new FormData();
      fd.append('file', file);
    return new RSVP.Promise(function(resolve, reject) {
            $.ajax({
              url: uploadObj.url,
              data: fd,
              processData: false,
              contentType: false,
              crossDomain: true,
              type: 'PUT',
              success: function(json, textStatus, jqXhr){
                console.log(json);
                resolve(json);
              },
              error: function(jqXhr, textStatus, errorThrown){
                reject({ jqXhr: jqXhr, textStatus: textStatus, errorThrown: errorThrown});
              }
            });
          });

UPDATE: In response to some of the comments, I did put in a valid CORS for the bucket.

<?xml version="1.0" encoding="UTF-8"?>
<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
    <CORSRule>
        <AllowedOrigin>*</AllowedOrigin>
        <AllowedMethod>GET</AllowedMethod>
        <AllowedMethod>PUT</AllowedMethod>
        <AllowedMethod>POST</AllowedMethod>
        <AllowedMethod>DELETE</AllowedMethod>
        <MaxAgeSeconds>3000</MaxAgeSeconds>
        <AllowedHeader>Content-*</AllowedHeader>
        <AllowedHeader>Authorization</AllowedHeader>
        <AllowedHeader>*</AllowedHeader>
    </CORSRule>
</CORSConfiguration>
4
  • Have you defined the appropriate CORS config for your bucket? If not, see the docs here. Commented Mar 6, 2015 at 13:46
  • Yea, I did. let me update that in my question Commented Mar 7, 2015 at 2:39
  • Would a wrong CORS? result in SignatureDoesNotMatch Commented Mar 7, 2015 at 3:37
  • THANK YOU for post this! I saw you commented on another question looking for a solution, then followed your profile to find this question. Hope my solution works for you Commented Apr 22, 2015 at 15:43

3 Answers 3

5

I've been fighting this as well. This is what worked for me and I was getting the exact same error as you.

On the server side, I'm using AWS-SDK for nodejs

var params = {
    Bucket: "bucketname",
    Key: "filename", 
    ContentType: "multipart/form-data"
}
var url = s3.getSignedUrl('putObject', params, function(err, url) { 
    console.log(url);
}

Client Side

$.ajax({
    method: "PUT",
    headers: {"Content-Type": "multipart/form-data"},
    processData: false,
    url: "http://AWSURL?AWSAccessKeyId..."
})

Your cors looks right to me, the key was ensuring that the headers for Content-Type matched exactly

Sign up to request clarification or add additional context in comments.

Comments

2

Thanks a ton to jeremy and kungfoo! I was having the same issues, though an added wrinkle when using Python's boto library. If you're generating the URLs from Python, a minimum working config seems to be:

Python

Boto3 works but not Boto2!

import boto3
conn = boto3.client('s3', ...)
url = conn.generate_presigned_url('put_object', {
    'Bucket': my_bucket_name,
    'Key': my_bucket_key,
    'ContentType': 'my_content_type'
}, 300)

S3 (CORS)

The minimum required rules seem to be:

<CORSRule>
    <AllowedOrigin>*.mydomain.com</AllowedOrigin>
    <AllowedMethod>PUT</AllowedMethod>
    <MaxAgeSeconds>3000</MaxAgeSeconds>
    <AllowedHeader>Content-*</AllowedHeader>
    <AllowedHeader>Authorization</AllowedHeader>
</CORSRule>

Client

Using jQuery, and being sure to keep the same content type as before:

$.ajax({
    type: "PUT",
    headers: {"Content-Type": "my_content_type"},
    url: url_from_server,
    data: my_data_as_string,
    processData: false,
    contentType: false,
    crossDomain: true,
    cache: false
});

1 Comment

This 'headers: {"Content-Type": "my_content_type"},' saved me
0

I had the same problem but I don't remember exactly what the problem was but this answer may help you upload-file-from-angularjs-directly-to-amazon-s3-using-signed-url.

Server:

var AWS = require('aws-sdk');

AWS.config.update({accessKeyId: AWS_ACCESS_KEY, secretAccessKey:     AWS_SECRET_KEY});
AWS.config.region = 'eu-west-1';

app.post('/s', function (req, res) {
    var s3 = new AWS.S3();
    var params = {Bucket: 'BUCKETNAME', Key: req.body.name, ContentType: req.body.type};
    s3.getSignedUrl('putObject', params, function(err, url) {
        if(err) console.log(err);
        res.json({url: url});
    });
});

Client:

$.ajax({
    url: '/s',
    type: 'POST',
    data: {name: file.name, size: file.size, type:file.type},
}).success(function(res){
    $.ajax({
        url: res.url,
        type: 'PUT',
        data: file,
        processData: false,
        contentType: file.type,
    }).success(function(res){
        console.log('Done');
    });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.