无法检索Amazon凭据

时间:2016-01-29 00:03:52

标签: java spring amazon-web-services pivotal-web-services

我正在尝试为托管在云上的Spring引导maven应用程序(Pivotal Web Services)提供凭据,以便能够上传到Amazon S3。但是,每次我尝试使用以下命令获取凭据时:

DefaultAWSCredentialsProviderChain credentialProviderChain = new DefaultAWSCredentialsProviderChain();
TransferManager transferManager = new TransferManager(credentialProviderChain.getCredentials());

它说

Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception 
[Request processing failed; nested exception is com.amazonaws.AmazonClientException: Unable
to load AWS credentials from any provider in the chain] with root cause
2016-01-28T23:55:38.55+0000 [APP/0]      OUT com.amazonaws.AmazonClientException: 
Unable to load AWS credentials from any provider in the chain

我是否应该在beans.xml或application.properties文件中定义我的凭据有点困惑。但我试过两个都无济于事。有人能指出我正确的方向吗?

2 个答案:

答案 0 :(得分:11)

查看文档! http://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/com/amazonaws/auth/DefaultAWSCredentialsProviderChain.html

AWS凭证提供程序链,按此顺序查找凭据:

  • 环境变量 - AWS_ACCESS_KEY_ID和AWS_SECRET_ACCESS_KEY(推荐,因为它们被除.NET以外的所有AWS开发工具包和CLI识别),或AWS_ACCESS_KEY和AWS_SECRET_KEY(仅由Java SDK识别)
  • Java系统属性 - aws.accessKeyId和aws.secretKey
  • 所有AWS开发工具包和AWS CLI共享的默认位置(〜/ .aws / credentials)的凭据配置文件
  • 通过Amazon EC2元数据服务提供的实例配置文件凭据

此处提供更多通用文档:http://docs.aws.amazon.com/AWSSdkDocsJava/latest/DeveloperGuide/credentials.html

答案 1 :(得分:3)

这种方法有效:

$ export AWS_ACCESS_KEY_ID=...
$ export AWS_SECRET_ACCESS_KEY=...