Java 无法从类路径上的 /AwsCredentials.properties 文件加载 AWS 凭证
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22588733/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Unable to load AWS credentials from the /AwsCredentials.properties file on the classpath
提问by Crew HaXor
Using this code for setting the class path
使用此代码设置类路径
AWSCredentialsProvider credentialsProvider = new ClasspathPropertiesFileCredentialsProvider();
ec2 = new AmazonEC2Client(credentialsProvider);
Below is the format for AwsCredentials.properties file
以下是 AwsCredentials.properties 文件的格式
# Fill in your AWS Access Key ID and Secret Access Key
# http://aws.amazon.com/security-credentials
accessKey = keyHere
secretKey = secretKeyHere
Below is the exception I am getting
下面是我得到的例外
Exception in thread "main" com.amazonaws.AmazonClientException: Unable to load AWS credentials from the /AwsCredentials.properties file on the classpath
at com.amazonaws.auth.ClasspathPropertiesFileCredentialsProvider.getCredentials(ClasspathPropertiesFileCredentialsProvider.java:81)
at com.amazonaws.services.ec2.AmazonEC2Client.invoke(AmazonEC2Client.java:8359)
采纳答案by farhangdon
You are getting this exception because your AWS SDK is unable to load your credentials. What you should do is goto Preferences then goto AWS and add your secret key and access key. So that your project can retrieve both keys.
您收到此异常是因为您的 AWS 开发工具包无法加载您的凭证。您应该做的是转到首选项,然后转到 AWS 并添加您的密钥和访问密钥。这样您的项目就可以检索这两个密钥。
回答by Simon
Try this for the file format:
试试这个文件格式:
[default]
aws_access_key_id=<your access key>
aws_secret_access_key=<your secret access key>
I saved this file as ~/.aws/credentials with ProfileCredentialsProvider().
我使用 ProfileCredentialsProvider() 将此文件保存为 ~/.aws/credentials。
回答by gbonesso
I made the connection using a different approach:
我使用不同的方法建立了连接:
BasicAWSCredentials credentials = new BasicAWSCredentials(ACCESS_KEY, SECRET_KEY);
AmazonDynamoDBClient client = new AmazonDynamoDBClient(credentials).withRegion(Regions.US_EAST_1);
DynamoDB dynamoDB = new DynamoDB(client);
The access key and the secret key can be created in the Identity and Access Managementconsole. I hope it helps...
可以在Identity and Access Management控制台中创建访问密钥和秘密密钥。我希望它有帮助...
回答by SheoSinha
If you use the credential file at ~/.aws/credentials and use the default profile as below:
如果您使用 ~/.aws/credentials 中的凭证文件并使用默认配置文件,如下所示:
[default]
aws_access_key_id=<your access key>
aws_secret_access_key=<your secret access key>
You do not need to use BasicAWSCredential
or AWSCredentialsProvider
. The SDK can pick up the credentials from the default profile, just by initializing the client object with the default constructor. Example below:
您不需要使用BasicAWSCredential
或AWSCredentialsProvider
。SDK 可以从默认配置文件中获取凭据,只需使用默认构造函数初始化客户端对象即可。下面的例子:
AmazonEC2Client ec2Client = new AmazonEC2Client();
In addition sometime you would need to initialize the client with the ClientConfiguration to provide proxy settings etc. Example below.
此外,有时您需要使用 ClientConfiguration 初始化客户端以提供代理设置等。示例如下。
ClientConfiguration clientConfiguration = new ClientConfiguration();
clientConfiguration.setProxyHost("proxyhost");
clientConfiguration.setProxyPort(proxyport);
AmazonEC2Client ec2Client = new AmazonEC2Client(clientConfiguration);
回答by Anders B
AWSCredentialsProvider credentialsProvider = new ProfileCredentialsProvider();
new AmazonEC2Client(credentialsProvider)
.aws/credentials
.aws/凭证
[default]
aws_access_key_id =
aws_secret_access_key =
回答by Raj Hassani
You can use DefaultAwsCredentialsProviderChain()
, which according to the documentation, looks for credentials in this order:
您可以使用DefaultAwsCredentialsProviderChain()
,根据文档,按以下顺序查找凭据:
- Environment Variables -
AWS_ACCESS_KEY_ID
andAWS_SECRET_ACCESS_KEY
(recommendedsince they are recognized by all AWS SDKs and CLI except for .NET), orAWS_ACCESS_KEY
andAWS_SECRET_KEY
(only recognized by the Java SDK) - Java System Properties -
aws.accessKeyId
andaws.secretKey
- Credential profiles file at the default location (
~/.aws/credentials
) shared by all AWS SDKs and the AWS CLI - Instance profile credentials delivered through the Amazon EC2 metadata service
- 环境变量-
AWS_ACCESS_KEY_ID
和AWS_SECRET_ACCESS_KEY
(推荐,因为它们是由所有AWS SDK和CLI除了.NET识别),或AWS_ACCESS_KEY
和AWS_SECRET_KEY
(仅由Java SDK识别) - Java 系统属性 -
aws.accessKeyId
和aws.secretKey
- 默认位置 (
~/.aws/credentials
) 中的凭证配置文件由所有 AWS 开发工具包和 AWS CLI 共享 - 通过 Amazon EC2 元数据服务交付的实例配置文件凭证
回答by Sai
Since AmazonDynamoDBClient(credentials) is deprecated i use this.
由于 AmazonDynamoDBClient(credentials) 已被弃用,我使用它。
init {
val cp= AWSStaticCredentialsProvider(BasicAWSCredentials(ACCESS_KEY, SECRET_KEY))
val client = AmazonDynamoDBClientBuilder.standard().withCredentials(cp).withRegion(Regions.US_EAST_1).build()
dynamoDB = DynamoDB(client)
}
回答by Nilesh
In a Linux server, using default implementation of ses
will expect files in .aws/credentials
file. You can put following content in credential file at the location below and it will work. /home/local/<your service account>/.aws/credentials
.
在 Linux 服务器中,使用默认实现ses
将期望文件中的.aws/credentials
文件。您可以将以下内容放在下面位置的凭证文件中,它会起作用。/home/local/<your service account>/.aws/credentials
.
[default]
aws_access_key_id=<your access key>
aws_secret_access_key=<your secret access key>
回答by user2677034
If you're wanting to use Environment variables using apache/tomcat, I found that the only way they could be found was setting them in tomcat/bin/setenv.sh (where catalina_opts are set - might be catalina.sh in your setup)
如果您想使用 apache/tomcat 使用环境变量,我发现可以找到它们的唯一方法是在 tomcat/bin/setenv.sh 中设置它们(设置 catalina_opts 的位置 - 可能是您的设置中的 catalina.sh)
export AWS_ACCESS_KEY_ID=*********;
导出 AWS_ACCESS_KEY_ID=*******;
export AWS_SECRET_ACCESS_KEY=**************;
导出 AWS_SECRET_ACCESS_KEY=**************;
If you're using ubuntu, try logging in as ubuntu $printenv then log in as root $printenv, the environmental variables won't necessarily be the same....
如果您使用的是 ubuntu,请尝试以 ubuntu $printenv 身份登录,然后以 root $printenv 身份登录,环境变量不一定相同....
If you only want to use environmental variables you can use: com.amazonaws.auth.EnvironmentVariableCredentialsProvider
如果您只想使用环境变量,您可以使用:com.amazonaws.auth.EnvironmentVariableCredentialsProvider
instead of:
代替:
com.amazonaws.auth.DefaultAWSCredentialsProviderChain
com.amazonaws.auth.DefaultAWSCredentialsProviderChain
(which by default checks all 4 possible locations)
(默认情况下会检查所有 4 个可能的位置)
anyway after hours of trying to figure out why my environmental variables weren't being found...this worked for me.
无论如何,经过数小时的尝试弄清楚为什么我的环境变量没有被发现......这对我有用。
回答by Red Boy
There are many correct answer above. Specifically in Windows, when you don't have ~/.aws/ folder exist and you need to create the new one, it turned out to be another problem, meaning if you just type ".aws" as name, it will error out and will not allow you create the folder with name ".aws".
上面有很多正确答案。特别是在 Windows 中,当你没有 ~/.aws/ 文件夹存在而你需要创建新的文件夹时,结果是另一个问题,这意味着如果你只输入“ .aws”作为名称,它会出错并且不允许您创建名为“.aws”的文件夹。
Here is trick to overcome that, i.e. type in ".aws."meaning dot at the start and dot at the end. Then only windows will accept the name. This has happened with me, so providing an answer here. SO that it may be helpful to others.
这是克服这个问题的技巧,即输入“.aws”。意思是开头的点和结尾的点。那么只有windows会接受这个名字。这发生在我身上,所以在这里提供答案。所以它可能对其他人有帮助。