报错:Received an UnknownHostException when attempting to interact with a service. See cause for the ex

场景:在访问amazon S3时遇到报错Exception in thread "main" software.amazon.awssdk.core.exception.SdkClientException: Received an UnknownHostException when attempting to interact with a service. See cause for the exact endpoint that is failing to resolve. If this is happening on an endpoint that previously worked, there may be a network connectivity issue or your DNS cache could be storing endpoints for too long.

// 设置认证信息
    static {
        AwsCredentials credentials = AwsBasicCredentials.create(accessKeyId, secretAccessKey);
        s3Client = S3Client.builder()
                .endpointOverride(URI.create(endpoint))
                .region(Region.of(region)) // 根据实际情况设置 region
                .credentialsProvider(StaticCredentialsProvider.create(credentials))
                .build();
    }

已解决:问题在设置认证信息时访问S3无法解析端点,主要原因: AWS S3(Simple Storage Service)有两种对象访问路径样式: 虚拟主机路径样式(默认):在虚拟主机路径样式中,S3桶的访问通过URL中的主机名来指定,例如https://bucket-name.s3.amazonaws.com/key-name。 路径样式:在路径样式中,S3桶的访问通过URL路径中的查询参数来指定,例如https://s3.amazonaws.com/bucket-name/key-name。

解决方法:使用.forcePathStyle(true)方法告诉S3客户端,无论是使用默认的虚拟主机路径样式还是路径样式,都强制使用路径样式。

// 设置认证信息
    static {
        AwsCredentials credentials = AwsBasicCredentials.create(accessKeyId, secretAccessKey);
        s3Client = S3Client.builder()
                .forcePathStyle(true)
                .endpointOverride(URI.create(endpoint))
                .region(Region.of(region)) // 根据实际情况设置 region
                .credentialsProvider(StaticCredentialsProvider.create(credentials))
                .build();
    }

  • 7
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

达芬奇要当程序员

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值