I'm using AWS Java SDK provided by Amazon to interact with the S3 service.
It seems that by default, the SDK uses virtual-host-style for buckets (i.e. buckets are reffered by bucket-name.s3.amazonaws.com. Example:
PUT / HTTP/1.1
Host: a-given-bucket.s3.amazonaws.com
Date: Tue, 26 Jun 2012 10:39:40 GMT
Content-Type: application/x-www-form-urlencoded; charset=utf-8
Content-Length: 0
However, I need to use path-style in my application, as follows:
PUT /a-given-bucket/ HTTP/1.1
Host: s3.amazonaws.com
Date: Thu, 21 Jun 2012 16:27:32 GMT
Content-Type: application/x-www-form-urlencoded; charset=utf-8
Content-Length: 0
Is it possible to use path-style with the Java SDK, please? In positive case, how can I do it? I have look at ClientConfiguration and AmazonS3Client classes but I don't see any method to do it...
My SDK version, in the case it matters, is: 2.0.0v201206151133.
Thanks!
Fermín
PD. Some headers have been omitted in the samples for the sake of simplicity.
EDIT:
Such a feature (to configure the URL path style used by the AmazonS3Client) is quite useful in case you have buckets with a dot (".") in them. HTTPS requests with Virtual-host-style do not work, see this and this.
The method withPathStyleAccess has been deprecated. Please use the following instead:
AmazonS3 s3client = AmazonS3Client.builder()
.withCredentials((new AWSStaticCredentialsProvider(credentials)))
.withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration("host", "region"))
.withPathStyleAccessEnabled(true)
.build();
Deprecated method:
This is now possible, I'm not sure when it was introduced, but it's available in at least the 1.7.8 version of the Java AWS SDK.
Just call setClientOptions on your AmazonS3 instance:
AmazonS3 client = new AmazonS3Client(credentials);
client.setS3ClientOptions(new S3ClientOptions().withPathStyleAccess(true));
There's no way to force V1 (path-style) bucket addressing using the Java SDK. The only exception is when your bucket name isn't DNS addressable, in which case the SDK will automatically use V1 addressing. This happens, e.g., when your bucket name contains a period (which is discouraged for this reason).
If you want this functionality, you'll have to modify the AmazonS3Client
class to allow it.
https://github.com/amazonwebservices/aws-sdk-for-java/
However, I'm not sure I believe your claim that you "need" to use V1 bucket addressing. The SDK already handles all cases where V1 addressing is necessary -- or if you have found a case where it doesn't, please let us know in the forums.
https://forums.aws.amazon.com/forum.jspa?forumID=70