可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
Using this code for setting the class path
AWSCredentialsProvider credentialsProvider = new ClasspathPropertiesFileCredentialsProvider();
ec2 = new AmazonEC2Client(credentialsProvider);
Below is the format for AwsCredentials.properties file
# 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)
回答1:
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.
回答2:
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 Management console. I hope it helps...
回答3:
You can use DefaultAwsCredentialsProviderChain()
According to the docs! http://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/com/amazonaws/auth/DefaultAWSCredentialsProviderChain.html
AWS credentials provider chain that looks for credentials in this order:
- Environment Variables - AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY (RECOMMENDED since they are recognized by all the AWS SDKs and CLI except for .NET), or AWS_ACCESS_KEY and AWS_SECRET_KEY (only recognized by Java SDK)
- Java System Properties - aws.accessKeyId and aws.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
回答4:
AWSCredentialsProvider credentialsProvider = new ProfileCredentialsProvider();
new AmazonEC2Client(credentialsProvider)
.aws/credentials
[default]
aws_access_key_id =
aws_secret_access_key =
回答5:
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().
回答6:
If you use the credential file at ~/.aws/credentials and use the default profile as below:
[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:
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 = new ClientConfiguration();
clientConfiguration.setProxyHost("proxyhost");
clientConfiguration.setProxyPort(proxyport);
AmazonEC2Client ec2Client = new AmazonEC2Client(clientConfiguration);
回答7:
Since AmazonDynamoDBClient(credentials) is deprecated i use this.
init {
val cp= AWSStaticCredentialsProvider(BasicAWSCredentials(ACCESS_KEY, SECRET_KEY))
val client = AmazonDynamoDBClientBuilder.standard().withCredentials(cp).withRegion(Regions.US_EAST_1).build()
dynamoDB = DynamoDB(client)
}
回答8:
In Linux server, using default implementation of ses
will expect files in .aws/credential
file. You can put following content in credential file at below location and it will work. /home/local/<your service account>/.aws/credential
.
[default]
aws_access_key_id=<your access key>
aws_secret_access_key=<your secret access key>
回答9:
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)
export AWS_ACCESS_KEY_ID=*********;
export 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....
If you only want to use environmental variables you can use:
com.amazonaws.auth.EnvironmentVariableCredentialsProvider
instead of:
com.amazonaws.auth.DefaultAWSCredentialsProviderChain
(which by default checks all 4 possible locations)
anyway after hours of trying to figure out why my environmental variables weren't being found...this worked for me.