使用AWS sdk for java从AWS服务器获取现有密钥对
我正在尝试使用AWS sdk for java在AWS上创建EC2实例 下面是runinstance方法使用AWS sdk for java从AWS服务器获取现有密钥对,java,amazon-web-services,amazon-ec2,Java,Amazon Web Services,Amazon Ec2,我正在尝试使用AWS sdk for java在AWS上创建EC2实例 下面是runinstance方法 public static RunInstancesResult createInstaince() { RunInstancesRequest runInstancesRequest = new RunInstancesRequest(); runInstancesRequest.withImageId("ami-ca381398")
public static RunInstancesResult createInstaince() {
RunInstancesRequest runInstancesRequest = new RunInstancesRequest();
runInstancesRequest.withImageId("ami-ca381398")
.withInstanceType("t2.micro")
.withMinCount(1)
.withMaxCount(1)
.withKeyName("java-sdk")
.withSubnetId("subnet-8eca36f9")
.withSecurityGroupIds("sg-3f00a25a");
RunInstancesResult runInstancesResult = amazonEC2Client
.runInstances(runInstancesRequest);
return runInstancesResult;
}
在这里,我明确指定java sdk作为密钥对,但现在我希望用户能够选择aws服务器上可用的密钥对。我看到了一个函数getAMI,它从aws服务器获取AMI。有人能告诉我像getKey pair这样的函数是否可行吗?是您正在寻找的。该链接用于EC2API文档。您可能需要查找调用此API的aws java sdk方法。公共静态列表getKeyName(){
public static List<String> getKeyName() {
DescribeKeyPairsRequest dkpr = new DescribeKeyPairsRequest();
DescribeKeyPairsResult dkpresult =
amazonEC2Client.describeKeyPairs(dkpr);
List<KeyPairInfo> keyPairs = dkpresult.getKeyPairs();
List<String> keyPairNameList = new ArrayList<String>();
for (KeyPairInfo keyPairInfo : keyPairs) {
keyPairNameList.add(keyPairInfo.getKeyName());
}
for (int i = 0; i < keyPairs.size(); i++) {
System.out.println(keyPairNameList.get(i));
}
return keyPairNameList;
}
DescribeKeyPairsRequest dkpr=新的DescribeKeyPairsRequest();
DescribeKeyPairsResult dkpresult=
amazonEC2Client.describeKeyPairs(dkpr);
List keyPairs=dkpreslt.getKeyPairs();
List keyPairNameList=新建ArrayList();
for(KeyPairInfo KeyPairInfo:keyPairs){
添加(keyPairInfo.getKeyName());
}
对于(int i=0;i
这是它将返回键名数组列表的代码