英文:
Unable to execute HTTP request with aws sdk and localstack
问题
以下是翻译好的内容:
我正在尝试使用 AWS SDK 访问 localstack 的 S3 服务,它在 AWS 命令行中正常工作,但 AWS SDK 的行为很奇怪,它在 URL 前面添加了存储桶名称,并提到无法连接。
代码如下:
public void testS3() {
final String localStackS3URL = "http://localhost:4566";
final String REGION = "us-east-1";
final AwsClientBuilder.EndpointConfiguration endpoint = new AwsClientBuilder.EndpointConfiguration(localStackS3URL, REGION);
final AmazonS3 client = AmazonS3ClientBuilder.standard()
.withEndpointConfiguration(endpoint)
.build();
if(!client.doesBucketExistV2("test")){
client.createBucket("test");
}
}
有谁能帮我找出问题在哪里?它在 AWS 命令行中工作,但 AWS SDK 奇怪地在 URL 前面添加了存储桶名称。
谢谢您的帮助!
英文:
I'm trying to hit the localstack s3 service with aws sdk and it works well with aws cli but the aws sdk is behaving weird by adding the bucketname to the front of the url mentioning unable to connect.
[![INTELLIJ debug][1]][1]
Code is as below
public void testS3() {
final String localStackS3URL = "http://localhost:4566";
final String REGION = "us-east-1";
final AwsClientBuilder.EndpointConfiguration endpoint = new AwsClientBuilder.EndpointConfiguration(localStackS3URL, REGION);
final AmazonS3 client = AmazonS3ClientBuilder.standard()
.withEndpointConfiguration(endpoint)
.build();
if(!client.doesBucketExistV2("test")){
client.createBucket("test");
}
}
Can anyone help me what is wrong here ? It works with aws cli but the aws sdk is prefixing the bucket name strangely.
[![cmd aws cli][2]][2]
Thanks in advance
[1]: https://i.stack.imgur.com/wMI8D.png
[2]: https://i.stack.imgur.com/L0jLV.png
答案1
得分: 2
使用.withPathStyleAccessEnabled(true)
。这样它将使用路径中的存储桶名称,而不是<bucket>.<endpoint>
的形式。
英文:
Use .withPathStyleAccessEnabled(true)
.
So it will use the bucket name in path instead of <bucket>.<endpoint>
form.
答案2
得分: 0
尝试在构建 S3 客户端时添加 HTTP 客户端参数,对我有效。
httpClient(UrlConnectionHttpClient.builder().build())
英文:
try adding the http client parameter while building the S3 client it worked for me
httpClient(UrlConnectionHttpClient.builder().build())
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论