未找到AWS S3凭据提供程序(Java SDK)

时间:2018-06-13 21:46:23

标签: java maven amazon-web-services amazon-s3

尝试使用mvn test在我的计算机上构建项目时,Amazon S3客户端无法找到凭据和区域,这些凭据和区域存储在〜/ .aws / credentials和〜/ .aws /中分别配置。

使用mvn exec运行程序时,S3客户端正确进行身份验证,同一功能没有问题。

有谁知道为什么会这样?愿意考虑替代解决方案,因为我想将应用程序移动到docker容器而不想发送无论如何,该容器的凭据。

完整的错误消息:

com.airdev.serena.AWSTest  Time elapsed: 10.628 sec  <<< FAILURE!
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.airdev.serena.AWSTest.before(AWSTest.java:32)

~/.aws/config的内容:

[default]
region = us-west-1

不应该有任何权限问题 - ~/.aws/config文件是可读的,无论如何我用sudo运行测试套件。

编辑:

我尝试了以下构建S3客户端的方法,以及其他一些我没有代码的组合。

s3Client = AmazonS3ClientBuilder.defaultClient();&lt; - 适用于maven exec命令以及从Eclipse IDE运行时

s3Client = AmazonS3ClientBuilder.standard()
                                     .withRegion("us-west-1") // The first region to try your request against
                                     .withForceGlobalBucketAccessEnabled(true) // If a bucket is in a different region, try again in the correct region
                                     .build();

即使我设置环境变量或删除.aws文件夹并通过aws configure命令重新设置凭据,这些似乎都没有获取默认凭证链。

1 个答案:

答案 0 :(得分:1)

使用aws cli配置默认凭据提供程序,因为您可以使用maven exec进行选择,这可能不是原因。使用命令

aws configure

使用默认凭据提供程序

AmazonS3 s3Client = AmazonS3ClientBuilder.standard()
                       .withRegion(Regions.EU_WEST_1)
                       .build();

您还可以明确地在代码中提供密钥。由于您愿意使用docker,因此凭据需要位于docker环境中。在代码中使用

BasicAWSCredentials awsCreds = new BasicAWSCredentials("access_key_id","secret_key_id");
AmazonS3 s3Client = AmazonS3ClientBuilder.standard()
                        .withCredentials(new AWSStaticCredentialsProvider(awsCreds))
                        .build();
相关问题