Aws-sdk-js: Signature Does Not Match when uploading image

Created on 8 Nov 2016  路  4Comments  路  Source: aws/aws-sdk-js

I tried to make a post request to my NodeJS server and it return the response URL and I make PUT request on it. It pops up "Upload Done!" message, but the Amazon S3 shows PUT 400 Bad Request (Signature not match).

Server

app.post('/upload', function(req, res){
    var s3 = new AWS.S3();
    var params =  {
       Bucket: 'mybucket01',
       Key: req.body.Key,
       ContentType: req.body.ContentType,
       ACL: 'public-read'
    };
    s3.getSignedUrl('putObject', params, function(err, url){
        if(err) console.log(err);
           res.json({
              returnCode: 200,
              returnMessage: "Success!",
              url: url
        })
    })
})

Angular Controller

$scope.upload = function(file) {
  // Get The PreSigned URL
  $http.post("http://localhost:8888/upload",{ Key: file.name, ContentType: file.type })
  .success(function(resp) {
    // Perform The Push To S3
    $http.put(resp.url, file, {headers: {'Content-Type': file.type}})
      .success(function(resp) {
        //Finally, We're done
        alert('Upload Done!')
      })
      .error(function(resp) {
        alert("An Error Occurred Attaching Your File");
      });
  })
  .error(function(resp) {
    alert("An Error Occurred Attaching Your File");
  });
}

CORS

<CORSRule>
    <AllowedOrigin>*</AllowedOrigin>
    <AllowedMethod>PUT</AllowedMethod>
    <AllowedMethod>GET</AllowedMethod>
    <AllowedMethod>POST</AllowedMethod>
    <MaxAgeSeconds>3000</MaxAgeSeconds>
    <AllowedHeader>*</AllowedHeader>
</CORSRule>
guidance

Most helpful comment

@ChuntheQhai
Can you confirm from the browser that the Content-Type header you are passing matches the one used when generating the url? It might be worth excluding the Content-Type from the url that is generated to see if the url then works. You can also try setting your S3 client to use sigv4. This may help narrow things down as the sigv4 signer works differently than the sigv2 (default for most regions) signer.

var s3 = new AWS.S3({signatureVersion: 'v4'});

All 4 comments

@ChuntheQhai
Can you confirm from the browser that the Content-Type header you are passing matches the one used when generating the url? It might be worth excluding the Content-Type from the url that is generated to see if the url then works. You can also try setting your S3 client to use sigv4. This may help narrow things down as the sigv4 signer works differently than the sigv2 (default for most regions) signer.

var s3 = new AWS.S3({signatureVersion: 'v4'});

@ChuntheQhai @chrisradek I had the same issue, a few days ago it worked with the default settings, but recently it gave the same "SignatureDoesNotMatch" response. I changed the signature version to v4 as @chrisradek mentioned and this did the trick for me.

Closing the old issues. Since the issue seems to already been solved by @chrisradek response. If the issue still exists please open a new issue.

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs and link to relevant comments in this thread.

Was this page helpful?
0 / 5 - 0 ratings