Ndkachare
Ndkachare

Reputation: 53

403 Signature not matched in uploading a file to AWS S3 with Pre-Signed URL and ng-file-upload from Browser

I am using ng-file-upload to send a file to AWS-S3 in my angular app.

Upload.http({
  url: '/presignedurl',
  headers : {
    'Content-Type': file.type
  },
  data: file
})

It is giving me 403 Forbidden error saying

<Code>SignatureDoesNotMatch</Code>
<Message>The request signature we calculated does not match the signature you provided. Check your key and signing method.</Message>

Upvotes: 2

Views: 1290

Answers (2)

Saurabh Joshi
Saurabh Joshi

Reputation: 267

You can try something like this

var config = {
    url: result.signed_request,
    headers: {
        "Content-Type": files[0].type != '' files[0].type : 'application/octet-stream'
    },
    method: 'PUT',
    data: files[0]
};
Upload.http(config);

Upvotes: 0

AWS S3 needs binary/octet-stream so you can use FileReader class in JavaScript to convert file data to binary/octet-stream

Replace your code with this

var reader = new FileReader();
var xhr = new XMLHttpRequest();

xhr.open("PUT", $scope.url);
reader.onload = function(evt) {
   xhr.send(evt.target.result);
};
reader.readAsArrayBuffer($files[file]);

Upvotes: 3

Related Questions