AWS V4 authentication requires that you know the bucket area so that you can sign the request correctly. Otherwise:
HTTP/1.1 400 Bad Request <?xml version="1.0" encoding="UTF-8"?> <Error> <Code>AuthorizationHeaderMalformed</Code> <Message>The authorization header is malformed; the region 'us-east-1' is wrong; expecting 'us-west-2'</Message> <Region>us-west-2</Region> <RequestId>xxxx</RequestId> <HostId>xxxx</HostId> </Error>
V2 signatures were not region specific, so there was previously a simple way to find out the bucket area using a V2 request:
http://docs.aws.amazon.com/AmazonS3/latest/API/RESTBucketGETlocation.html
However, there is suspicion of a trick-22 with V4, since you need to know the bucket area before you can make a call to discover the bucket area.
So the first solution is to capture the <Region> returned in the error response and use this area to sign future requests for the bucket. Obviously, you will want to cache this information, as this will not lead to reduced performance and increased costs.
Alternatively, there is a way to ask the US-Standard (us-east-1) region about the location of any bucket in any region using this URL format only :
https:
Sign this request in the us-east-1 region and you will get an answer wherever the bucket is. Note that if the bucket is in us-east-1 (US-Standard), then LocationConstraint returned empty.
<?xml version="1.0" encoding="UTF-8"?> <LocationConstraint xmlns="http://s3.amazonaws.com/doc/2006-03-01/"> us-west-2 </LocationConstraint>
You cannot use this URL construct to actually list bucket objects in other regions, since https://s3.amazonaws.com/ always routes your request to US-Standard (us-east-1), but you can use it to detecting the area of ββany bucket, as US-Standard has this information.
Update / Additonal
At some point, S3 adds a new x-amx-bucket-region: response header, which appears to be an undocumented addition to the REST API, and appears to be added to many of the responses to S3's error, in particular 403 errors.
This seems like a useful self-correction mechanism if you get an error response.
In addition, the information above on how to poll the US-Standard region about the location of any bucket anywhere is still accurate, and the same request should work if it is sent to any regional REST endpoint of region S3, and not just to US-Standard, because all regions know the location of all the other buckets: for example, https://s3-us-west-2.amazonaws.com/bucket-name?location would ask US-West-2 (Oregon), which also has the same information as for any bucket around the world. In some cases, you may need to survey your nearest region.