Update
I have bad news. According to release notes of SDK 2.1.6 at http://aws.amazon.com/releasenotes/1473534964062833:
"The SDK will now throw an error if ContentLength is passed into an
Amazon S3 presigned URL (AWS.S3.getSignedUrl()). Passing a
ContentLength is not supported by the SDK, since it is not enforced on
S3's side given the way the SDK is currently generating these URLs.
See GitHub issue #457."
I have found on some occassions, ContentLength must be included (specifically if your client passes it so the signatures will match), then on other occassions, getSignedUrl will complain if you include ContentLength with a parameter error: "contentlength is not supported in presigned urls". I noticed that the behavior would change when I changed the machine which was making the call. Presumably the other machine made a connection to another Amazon server in the farm.
I can only guess why the behavior exists in some cases, but not in others. Perhaps not all of Amazon's servers have been fully upgraded? In either case, to handle this problem, I now make an attempt using ContentLength and if it gives me the parameter error, then I call the getSignedUrl again without it. This is a work-around to deal with this strange behavior with the SDK.
A little example... not very pretty to look at but you get the idea:
MediaBucketManager.getPutSignedUrl = function ( params, next ) {
var _self = this;
_self._s3.getSignedUrl('putObject', params, function ( error, data ) {
if (error) {
console.log("An error occurred retrieving a signed url for putObject", error);
// TODO: build contextual error
if (error.code == "UnexpectedParameter" && error.message.search("ContentLength") > -1) {
if (params.ContentLength) delete params.ContentLength
MediaBucketManager.getPutSignedUrl(bucket, key, expires, params, function ( error, data ) {
if (error) {
console.log("An error occurred retrieving a signed url for putObject", error);
} else {
console.log("Retrieved a signed url for putObject:", data);
return next(null, data)
}
});
} else {
return next(error);
}
} else {
console.log("Retrieved a signed url for putObject:", data);
return next(null, data);
}
});
};
So, below is not entirely correct (it will be correct in some cases but give you the parameter error in others) but might help you get started.
Old Answer
It seems (for a signedUrl to PUT a file to S3 where there is only public-read ACL) there are a few headers that will be compared when a request is made to PUT to S3. They are compared against what has been passed to getSignedUrl:
CacheControl: 'STRING_VALUE',
ContentDisposition: 'STRING_VALUE',
ContentEncoding: 'STRING_VALUE',
ContentLanguage: 'STRING_VALUE',
ContentLength: 0,
ContentMD5: 'STRING_VALUE',
ContentType: 'STRING_VALUE',
Expires: new Date || 'Wed De...'
see the full list here: http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#putObject-property
When you're calling getSignedUrl you'll pass a 'params' object (fairly clear in the documentation) that includes the Bucket, Key, and Expires data. Here is an (NodeJS) example:
var params = { Bucket:bucket, Key:key, Expires:expires };
s3.getSignedUrl('putObject', params, function ( error, data ) {
if (error) {
// handle error
} else {
// handle data
}
});
Less clear is setting the ACL to 'public-read':
var params = { Bucket:bucket, Key:key, Expires:expires, ACL:'public-read' };
Very much obscure is the notion of passing headers that you expect the client, using the signed url, will pass along with the PUT operation to S3:
var params = {
Bucket:bucket,
Key:key,
Expires:expires,
ACL:'public-read',
ContentType:'image/png',
ContentLength:7469
};
In my example above, I have included ContentType and ContentLength because those two headers are included when using XmlHTTPRequest in javascript, and in the case of Content-Length cannot be changed. I suspect that will be the case for other implementations of HTTP requests like Curl and such because they are required headers when submitting HTTP requests that include a body (of data).
If the client does not include the ContentType and ContentLength data about the file when requesting a signedUrl, when it comes time to PUT the file to S3 (with that signedUrl), the S3 service will find the headers included with the client's requests (because they are required headers) but the signature will not have included them - and so, they will not match and the operation will fail.
So, it appears that you will have to know, in advance of making your getSignedUrl call, the content type and content length of the file to be PUT to S3. This wasn't a problem for me because I exposed a REST endpoint to allow our clients to request a signed url just before making the PUT operation to S3. Since the client has access to the file to be submitted (at the moment they are ready to submit), it was a trivial operation for the client to access the file size and type and request a signed url with that data from my endpoint.