本文整理汇总了Java中com.amazonaws.services.ec2.AmazonEC2.createKeyPair方法的典型用法代码示例。如果您正苦于以下问题:Java AmazonEC2.createKeyPair方法的具体用法?Java AmazonEC2.createKeyPair怎么用?Java AmazonEC2.createKeyPair使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.amazonaws.services.ec2.AmazonEC2
的用法示例。
在下文中一共展示了AmazonEC2.createKeyPair方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: main
import com.amazonaws.services.ec2.AmazonEC2; //导入方法依赖的package包/类
public static void main(String[] args)
{
final String USAGE =
"To run this example, supply a key pair name\n" +
"Ex: CreateKeyPair <key-pair-name>\n";
if (args.length != 1) {
System.out.println(USAGE);
System.exit(1);
}
String key_name = args[0];
final AmazonEC2 ec2 = AmazonEC2ClientBuilder.defaultClient();
CreateKeyPairRequest request = new CreateKeyPairRequest()
.withKeyName(key_name);
CreateKeyPairResult response = ec2.createKeyPair(request);
System.out.printf(
"Successfulyl created key pair named %s",
key_name);
}
示例2: createKeyValuePair
import com.amazonaws.services.ec2.AmazonEC2; //导入方法依赖的package包/类
/***
* Creates a 2048-bit RSA key pair with the specified name
*
* @param keyName Key name to use
* @return Unencrypted PEM encoded PKCS#8 private key
*/
public String createKeyValuePair(String keyName) {
final AmazonEC2 amazonEC2 = getEc2Client();
final CreateKeyPairRequest createKeyPairRequest = new CreateKeyPairRequest().withKeyName(keyName);
final CreateKeyPairResult createKeyPairResult = amazonEC2.createKeyPair(createKeyPairRequest);
final KeyPair keyPair = createKeyPairResult.getKeyPair();
final String material = keyPair.getKeyMaterial();
LOGGER.info("Created key: " + keyName);
LOGGER.debug("Created material: " + material);
return material;
}
示例3: submitEC2Key
import com.amazonaws.services.ec2.AmazonEC2; //导入方法依赖的package包/类
@Action(value = "/manage/submitEC2Key",
results = {
@Result(name = "input", location = "/manage/view_ec2_keys.jsp"),
@Result(name = "success", location = "/manage/viewEC2Keys.action", type = "redirect")
}
)
public String submitEC2Key() {
String retVal = SUCCESS;
try {
//get AWS credentials from DB
AWSCred awsCred = AWSCredDB.getAWSCred(ec2Key.getAwsCredId());
//set AWS credentials for service
BasicAWSCredentials awsCredentials = new BasicAWSCredentials(awsCred.getAccessKey(), awsCred.getSecretKey());
//create service
AmazonEC2 service = AmazonEC2ClientBuilder.standard()
.withCredentials(new AWSStaticCredentialsProvider(awsCredentials))
.withClientConfiguration(AWSClientConfig.getClientConfig())
.withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(ec2Key.getEc2Region(), ec2RegionMap.get(ec2Key.getEc2Region()))).build();
//create key pair request
CreateKeyPairRequest createKeyPairRequest = new CreateKeyPairRequest();
createKeyPairRequest.withKeyName(ec2Key.getKeyNm());
//call service
CreateKeyPairResult createKeyPairResult = service.createKeyPair(createKeyPairRequest);
//get key pair result
KeyPair keyPair = createKeyPairResult.getKeyPair();
//set private key
String privateKey = keyPair.getKeyMaterial();
ec2Key.setPrivateKey(privateKey);
//add to db
EC2KeyDB.saveEC2Key(ec2Key);
} catch (AmazonServiceException ex) {
addActionError(ex.getMessage());
retVal = INPUT;
}
return retVal;
}