Example usage for com.amazonaws.services.ec2.model KeyPair toString

List of usage examples for com.amazonaws.services.ec2.model KeyPair toString

Introduction

In this page you can find the example usage for com.amazonaws.services.ec2.model KeyPair toString.

Prototype

@Override
public String toString() 

Source Link

Document

Returns a string representation of this object.

Usage

From source file:edu.umass.cs.aws.support.AWSEC2.java

License:Apache License

/**
 * Create a New Key Pair//w  w w. j av  a 2 s. com
 *
 * @param ec2
 * @param name
 * @return the keypair
 */
public static KeyPair createKeyPair(AmazonEC2 ec2, String name) {
    CreateKeyPairRequest newKeyRequest = new CreateKeyPairRequest();
    newKeyRequest.setKeyName(name);
    CreateKeyPairResult keyresult = ec2.createKeyPair(newKeyRequest);

    /**
     * **********************print the properties of this key****************
     */
    KeyPair keyPair = keyresult.getKeyPair();
    System.out.println("The key we created is = " + keyPair.toString());

    /**
     * ***************store the key in a .pem file ***************
     */
    try {
        String fileName = KEYHOME + FILESEPARATOR + name + PRIVATEKEYFILEEXTENSION;
        File distFile = new File(fileName);
        BufferedReader bufferedReader = new BufferedReader(new StringReader(keyPair.getKeyMaterial()));
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(distFile));
        char buf[] = new char[1024];
        int len;
        while ((len = bufferedReader.read(buf)) != -1) {
            bufferedWriter.write(buf, 0, len);
        }
        bufferedWriter.flush();
        bufferedReader.close();
        bufferedWriter.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return keyPair;
}