I want to upload video files from browser to S3. Here is the my steps:
- Send
GETrequest to API Gateway, invoke Lambda to get a signed URL. - Use the signed URL directly to upload files to S3 using
PUT.
Step 1 works fine, I can use the signed URL to upload file through curl:
curl -X PUT https://bucket-name.s3.amazonaws.com/testupload/videofile.mp4?AWSAccessKeyId=MY_AWS_ACCESS_KEY_ID&Expires=1523329853&Signature=XXXXX -T videophile.mp4
But upload the file to S3 from browser, I get 403 response with SignatureDoesNotMatch error:
<Error>
<Code>SignatureDoesNotMatch</Code>
<Message>The request signature we calculated does not match the signature you provided. Check your key and signing method.</Message>
<AWSAccessKeyId>MY_AWS_ACCESS_KEY_ID</AWSAccessKeyId>
...
...
</Error>
I checked the access key is the one I used in lambda to create signed url
Client side Javascript:
$.ajax({
url : signedurl,
type : "PUT",
data : uploadfile,
dataType : "text",
cache : false,
contentType : file.type,
processData : false
})
.done(function() {
})
.fail(function(e) {
console.error(e.status, e.statusText)
console.error(e.responseText)
});
Bucket CORS config:
<?xml version="1.0" encoding="UTF-8"?>
<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
<CORSRule>
<AllowedOrigin>*</AllowedOrigin>
<AllowedMethod>GET</AllowedMethod>
<AllowedMethod>POST</AllowedMethod>
<AllowedMethod>PUT</AllowedMethod>
<AllowedMethod>HEAD</AllowedMethod>
<AllowedMethod>DELETE</AllowedMethod>
<MaxAgeSeconds>3000</MaxAgeSeconds>
<AllowedHeader>*</AllowedHeader>
</CORSRule>
</CORSConfiguration>