AWS 无法验证提供的访问凭证 (eclipse)
AWS cannot validate the provided access credentials (eclipse)
我一直在使用 Java 的 AWS SDK 通过简单的 Java 程序启动亚马逊 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;
}
}
谁能帮帮我?
我认为您的程序有些混乱 - 当您这样做时:
AWSCredentials credentials=null;
credentials = new BasicAWSCredentials("****","****");
credentials = new ProfileCredentialsProvider().getCredentials();
行 credentials = new BasicAWSCredentials("****","****");
最终不再是必需的,因为将设置凭据并在替换为来自 ProfileCredentialsProvider 的凭据后。
您不能同时使用BasicAWSCredentials
和ProfileCredentialsProvider
当您使用 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 不应该是静态的。 (在简单的程序中它可能没问题,但随着你的基础设施不断增长(多区域,实例......),这是不可能的)
我一直在使用 Java 的 AWS SDK 通过简单的 Java 程序启动亚马逊 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;
}
}
谁能帮帮我?
我认为您的程序有些混乱 - 当您这样做时:
AWSCredentials credentials=null;
credentials = new BasicAWSCredentials("****","****");
credentials = new ProfileCredentialsProvider().getCredentials();
行 credentials = new BasicAWSCredentials("****","****");
最终不再是必需的,因为将设置凭据并在替换为来自 ProfileCredentialsProvider 的凭据后。
您不能同时使用BasicAWSCredentials
和ProfileCredentialsProvider
当您使用 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 不应该是静态的。 (在简单的程序中它可能没问题,但随着你的基础设施不断增长(多区域,实例......),这是不可能的)