AWS无法验证提供的访问凭据(eclipse)

时间:2015-09-25 09:43:25

标签: java amazon-web-services credentials aws-sdk

我一直在使用AWS SDK for Java通过简单的Java程序启动amazon EC2实例。我在代码本身中提供了我的秘密和访问密钥,并在windows>preferences>aws sdk中也提供了密钥。我还将我的凭据添加到我正在使用的密钥中。我仍然收到此错误

Exception in thread "main" com.amazonaws.AmazonServiceException: AWS was not able to validate the provided access credentials (Service: AmazonEC2; Status Code: 401; Error Code: AuthFailure; Request ID: cc82e725-5a8a-44d8-9f7c-2031d6db1e1a)
at com.amazonaws.http.AmazonHttpClient.handleErrorResponse(AmazonHttpClient.java:1182)
at com.amazonaws.http.AmazonHttpClient.executeOneRequest(AmazonHttpClient.java:770)
at com.amazonaws.http.AmazonHttpClient.executeHelper(AmazonHttpClient.java:489)
at com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:310)
at com.amazonaws.services.ec2.AmazonEC2Client.invoke(AmazonEC2Client.java:11819)
at com.amazonaws.services.ec2.AmazonEC2Client.startInstances(AmazonEC2Client.java:6153)
at amazon.Amazon.main(Amazon.java:53)

这是我的Java代码

 package amazon;

import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.auth.profile.ProfileCredentialsProvider;
import com.amazonaws.regions.Regions;
import com.amazonaws.regions.Region;
import com.amazonaws.services.ec2.AmazonEC2;
import com.amazonaws.services.ec2.AmazonEC2Client;
import com.amazonaws.services.ec2.model.StartInstancesRequest;
import com.amazonaws.services.ec2.model.StartInstancesResult;

public class Amazon 
{
static AmazonEC2 ec2;

    static void authentication() throws Exception {
    AWSCredentials credentials=null;
 credentials = new BasicAWSCredentials("****","****");
 credentials=new ProfileCredentialsProvider().getCredentials();

    System.out.println("Credentials : " + credentials);
    ec2 = new AmazonEC2Client(credentials);
    Region region=Region.getRegion(Regions.AP_SOUTHEAST_1);
    ec2.setRegion(region);

}

  public static void main(String[] args) throws Exception
  {
      authentication(); 
        StartInstancesRequest startInstancesRequest = new StartInstancesRequest();

             startInstancesRequest.withInstanceIds("i-sba78aj1");

   StartInstancesResult res = ec2.startInstances(startInstancesRequest); 
              System.out.println(res.toString());
              return;
      }
}

任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:4)

我认为您的计划存在一些混乱 - 当您这样做时:

AWSCredentials credentials=null;
credentials = new BasicAWSCredentials("****","****");
credentials = new ProfileCredentialsProvider().getCredentials();

credentials = new BasicAWSCredentials("****","****");最终不是必需的,因为凭证将被设置,并在替换为ProfileCredentialsProvider中的凭证之后。

您不能同时使用BasicAWSCredentialsProfileCredentialsProvider

当您使用ProfileCredentialsProvider时,您需要在~/.aws/credentials下拥有有效的凭据文件,该文件的格式应为

[default]
aws_access_key_id=XXXXX
aws_secret_access_key=XXXX

在这种情况下,你会做

    AWSCredentials credentials = null;
    try {
        credentials = new ProfileCredentialsProvider().getCredentials();
    } catch (Exception e) {
        throw new AmazonClientException(
                "Cannot load the credentials from the credential profiles file. " +
                "Please make sure that your credentials file is at the correct " +
                "location (~/.aws/credentials), and is in valid format.",
                e);
    }
    AmazonEC2 ec2 = new AmazonEC2Client(credentials);

如果您不使用凭据文件,则可以像BasicAWSCredentials那样使用API​​创建凭据:

AWSCredentials credentials = new BasicAWSCredentials("XXXXX", "XXXXX");
// no need to call the ProfileCredentialsProvider in this case
ec2 = new AmazonEC2Client(credentials);

只是一个简单的注释,我知道你可能会运行一个快速测试,你可以使用main方法,但是真的声明static AmazonEC2 ec2;是一个不好的做法,EC2Client不应该是静止的。 (在简单的程序中,它可以正常,但随着您的基础设施的增长(多个区域,实例......),这是不可能的)