Setting path style in Amazon S3 Java SDK

I am using the AWS Java SDK provided by Amazon to interact with the S3 service.

By default, the SDK seems to use a virtual host type for buckets (i.e. buckets are represented 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 the path style in my application as shown below:

 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 Java SDK please? In the positive case, how can I do this? I look at the ClientConfiguration and AmazonS3Client classes, but I don't see any methods for this ...

My version of the SDK, in case it matters, is: 2.0.0v201206151133.

Thanks!


Fermin

PD. Some headings are omitted for simplicity in the samples.

EDIT: This feature (for customizing the style of the URL used by AmazonS3Client) is very useful if you have buckets with a period (".") In them. HTTPS requests with a virtual host do not work, see this and this .

+6
source share
2 answers

The withPathStyleAccess method is deprecated. Use instead:

 AmazonS3 s3client = AmazonS3Client.builder() .withCredentials((new AWSStaticCredentialsProvider(credentials))) .withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration("host", "region")) .withPathStyleAccessEnabled(true) .build(); 

Deprecated Method:

Now it's possible, I'm not sure when it was introduced, but it is available at least in version 1.7.8 of the Java AWS SDK.

Just call setClientOptions on your AmazonS3 instance:

 AmazonS3 client = new AmazonS3Client(credentials); client.setS3ClientOptions(new S3ClientOptions().withPathStyleAccess(true)); 
+8
source

There is no way to force the V1 bucket (path-style) to be addressed using the Java SDK. The only exception is when your bucket name is not a DNS address, in which case the SDK will automatically use V1 addressing. This happens, for example, when your bucket name contains a period (which is therefore not recommended).

If you want this functionality, you will need to modify the AmazonS3Client class to enable it.

https://github.com/amazonwebservices/aws-sdk-for-java/

However, I'm not sure I believe in your statement that you need to use a V1 bucket. The SDK already handles all cases where V1 addressing is required, or if you find a case where this is not the case, let us know in the forums.

https://forums.aws.amazon.com/forum.jspa?forumID=70

0
source

Source: https://habr.com/ru/post/919140/


All Articles