I'm having problems uploading a file to Amazon S3. I've developed a Grails RESTful service which uses the AWS Java SDK to generate pre signed URLs. When the client uploads a file, it first retrieves a pre-signed URL and then uses this to upload the file directly to my S3 bucket. So I have a Grails service which creates a pre signed URL like so...
def generateFileUploadUrl(AmazonS3Client client, String bucketName, String key, int expiryMins) {
GeneratePresignedUrlRequest req = new GeneratePresignedUrlRequest(bucketName, key);
req.setMethod(HttpMethod.POST);
req.setExpiration(getExpiration(expiryMins));
return client.generatePresignedUrl(req);
}
And then the client retrieves a URL with the following format...
https://{bucketname}.amazonaws.com/{key}?AWSAccessKeyId={accesskey}&Expires={expiry}&Signature={signature}
Then the client creates a POST request using Danial Farid's Angular File Upload module like so...
Upload.upload({
url: destUrl, // url shown above
file: file
}).progress(function (evt) {
var progressPercentage = parseInt(100.0 * evt.loaded / evt.total);
console.log('progress: ' + progressPercentage + '% ' + evt.config.file.name);
}).success(function (data, status, headers, config) {
console.log('file ' + config.file.name + 'uploaded. Response: ' + data);
}).error(function (data, status, headers, config) {
console.log('error status: ' + status);
});
At first I received errors about CORS settings but after editing the CORS origin configuration in my Bucket's permissions, I started getting a 403 forbidden response instead. The message in the 403 response is 'The request signature we calculated does not match the signature you provided. Check your key and signing method.'. The AWS Access Key and the Signature provided match up so I'm not sure what the exact error is.
Is my request missing some extra information? Looked at a few other posts, like this, which manually creates a policy document to send with the URL but it doesn't use the AWS Java SDK.
As it happens, my approach works fine with GET requests, and I can retrieve documents. Just can't upload.
req.setMethod(HttpMethod.POST);... butPOST!=PUT...