-1

我正在尝试使用我的代码通过 aws sdk 和 java 服务访问 S3。但我收到以下异常。

我使用的代码如下。

  1. 配置生成器。

    ClientConfiguration clientConfiguration = new ClientConfiguration();
    clientConfiguration.setRetryPolicy( PredefinedRetryPolicies.getDefaultRetryPolicyWithCustomMaxRetries(5));
    
     AmazonS3ClientBuilder s3ClientCommonBuilder = AmazonS3ClientBuilder.standard()
         .withClientConfiguration(clientConfiguration)
         .withForceGlobalBucketAccessEnabled(true)
         .withPathStyleAccessEnabled(true);
    
         s3ClientCommonBuilder.withCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials(awsKey, awsSecret)));
    
    
     return s3ClientCommonBuilder;
    
  2. 创建 AwsS3Client

     AmazonS3ClientBuilder internalS3ClientBuilder = 
     createS3ClientCommonBuilder(s3bucket, awsKey, awsSecret);
     if (internalServiceEndpoint != null && !internalServiceEndpoint.isEmpty()) {
         internalS3ClientBuilder.withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(internalServiceEndpoint, Region.EU_Ireland.getFirstRegionId()));
     }
    

然后在运行时当客户端尝试连接到 s3 时抛出异常。

1) Error in custom provider, com.amazonaws.SdkClientException: Unable to find a region via the region provider chain. Must provide an explicit region in the builder or setup environment to supply a region.
while locating AwsS3ClientProvider
at org.test.ApiServerBootstrap.configure(ApiServerBootstrap.java:103)
while locating org.test.AWSS3Client
for the 3rd parameter of org.testDataUploader.<init>(DataUploader.java:60)
at org.test.DataUploader.class(DataUploader.java:49)
while locating org.test.DataUploader
for the 3rd parameter of org.test.StoreService.<init>(StoreService.java:56)
at org.test.services.v3.StoreService.class(StoreService.java:53)
while locating org.test.StoreService
for the 1st parameter of org.test.verticles.EventBusConsumerVerticle.<init> 
(EventBusConsumerVerticle.java:35)
at 
org.test.verticles.EventBusConsumerVerticle.class(EventBusConsumerVerticle.java:35)
while locating org.test.verticles.EventBusConsumerVerticle
for the 3rd parameter of org.test.ApiServer.<init>(ApiServer.java:52)
while locating org.test.ApiServer

1 error
at com.google.inject.internal.InternalProvisionException.toProvisionException(InternalProvisionException.java:226)
at com.google.inject.internal.InjectorImpl$1.get(InjectorImpl.java:1053)
at com.google.inject.internal.InjectorImpl.getInstance(InjectorImpl.java:1086)
at org.test.ApiServer.main(ApiServer.java:46)
Caused by: com.amazonaws.SdkClientException: Unable to find a region via the region provider chain. Must provide an explicit region in the builder or setup environment to supply a region.
at com.amazonaws.client.builder.AwsClientBuilder.setRegion(AwsClientBuilder.java:462)
at com.amazonaws.client.builder.AwsClientBuilder.configureMutableProperties(AwsClientBuilder.java:424)
at com.amazonaws.client.builder.AwsSyncClientBuilder.build(AwsSyncClientBuilder.java:46)
at org.test.AWSS3Client.create(AWSS3Client.java:99)

我已经用其他一些计算机对此进行了测试,以验证这是我的 aws 客户端配置错误。

我的 .aws/config 文件如下所示

$ cat .aws/config 
[default]
region = eu-west-1
output = json

我的 .aws/credentials 如下所示。

$ cat .aws/credentials 
[default]
aws_access_key_id=XXXXXXXX.....
aws_secret_access_key=XXXXXXXX

我的 aws 客户端版本如下。

$ aws --version
aws-cli/2.2.40 Python/3.8.8 Linux/5.11.0-34-generic exe/x86_64.ubuntu.20 prompt/off

如果有人能弄清楚我缺少什么让 aws 客户端连接到 s3,我将不胜感激。

4

1 回答 1

0

您要确保在构建 S3Client 时使用 DefaultAWSCredentialsProviderChain() 类。

您还可以创建一个环境变量 AWS_REGION=eu-west-1

于 2021-09-22T12:51:12.577 回答