使用此代码设置类路径
AWSCredentialsProvider credentialsProvider = new ClasspathPropertiesFileCredentialsProvider(); ec2 = new AmazonEC2Client(credentialsProvider);
以下是AwsCredentials.properties文件的格式
# Fill in your AWS Access Key ID and Secret Access Key # http://aws.amazon.com/security-credentials accessKey = keyHere secretKey = secretKeyHere
以下是我得到的例外
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)
因为您的AWS开发工具包无法加载您的凭证,所以您将收到此异常。您应该执行的操作是转到“首选项”,然后转到“ AWS”并添加您的秘密密钥和访问密钥。这样您的项目就可以检索两个密钥。