无法使用AWS SDK和LocalStack执行HTTP请求。

huangapple go评论60阅读模式
英文:

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 &lt;bucket&gt;.&lt;endpoint&gt; 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())

huangapple
  • 本文由 发表于 2023年2月14日 03:38:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/75440483.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定