在Java SDK Amazon S3中配置路径样式 [英] Configure path-style in Java SDK Amazon S3
问题描述
我正在使用亚马逊提供的 AWS Java SDK 进行互动使用S3服务。
I'm using AWS Java SDK provided by Amazon to interact with the S3 service.
默认情况下,SDK使用虚拟主机风格的存储桶(即存储桶由存储桶名称 .s3.amazonaws.com。示例:
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
是否可以在Java SDK中使用path-style?在正面情况下,如何才能我做到了吗?我查看了ClientConfiguration和AmazonS3Client类,但我没有看到任何方法来执行它...
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...
我的SDK版本,如果重要的话,是:2.0 .0v201206151133。
My SDK version, in the case it matters, is: 2.0.0v201206151133.
谢谢!
Fermín
PD。为简单起见,示例中省略了一些标题。
PD. Some headers have been omitted in the samples for the sake of simplicity.
编辑:
这样的功能(用于配置URL) AmazonS3Client使用的路径样式非常有用,如果你的桶中有一个点(。)。虚拟主机样式的HTTPS请求不起作用,请参阅这个和这个。
推荐答案
不推荐使用方法 withPathStyleAccess 。请改用以下代码:
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:
现在可以了,我不确定它何时推出,但它至少可以在1.7.8版本的Java AWS SDK中使用。
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.
只需致电 setClientOptions :
AmazonS3 client = new AmazonS3Client(credentials);
client.setS3ClientOptions(new S3ClientOptions().withPathStyleAccess(true));
这篇关于在Java SDK Amazon S3中配置路径样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!