jeudi 28 mai 2015

List down all the available Amazon machine images provided in the AWS market place and Amazon in Sydney region

//I am trying to list down all the available Amazon machine images provided in the AWS market place and Amazon in Sydney region.can anyone suggest me some idea. I am not sure whether answer i am getting is correct or not.I have done this :

public class AmazonAmi {
     public static final String AWS_CREDENTIALS_PROPERTIES_FILENAME = "AWSCredentials.properties";
     public static final String PUBLIC_ACCESS_KEY_ID = "accessKey";
     public static final String SECRET_ACCESS_KEY = "secretKey";
     public static final String AMI_FILE_NAME = "AmazonMachineImages.txt";
     public static final String LINE_SEPARATOR = "line.separator";


    /**
     * To list down all the available AMIs in Sydney region
     */
    private static void getAmazonMachineImages(){
        //get Amazon EC2 client
         AmazonEC2 ec2Client = getAWSEc2Client();
         //set Sydney region
         ec2Client.setRegion(Region.getRegion(Regions.AP_SOUTHEAST_2));
         //get all the images from Sydney region ec2 client
         DescribeImagesResult result  = ec2Client.describeImages();
         // get list of AMIs from the result
         List<Image> amiList = result.getImages();
         //write down the AMIs into a file
         writeAmisToFile(amiList);
    }

     /**
     * Gets the AWS credentials from the .aws-credentials file 
     * the user and then creates a ec2 client.
     *
     * @return client for communicating with AWS web services.
     */
    private static AmazonEC2 getAWSEc2Client(){
        String accessKey = null;
        String secretKey = null;
        Properties p = new Properties();
        String awsCredentialsFilename = AmazonAmi.AWS_CREDENTIALS_PROPERTIES_FILENAME;
        File awsCredentialsFile = new File(awsCredentialsFilename);
        if (!awsCredentialsFile.exists()) {
            System.out .println("\nAWS credentials file " + awsCredentialsFile  + " not found.");
            System.exit(-2);
        } else if (!awsCredentialsFile.canRead()) {
            System.out.println("Unable to read AWS Credentials file " + awsCredentialsFile);
            System.exit(-2);
        } else {
            try {
                p.load(new FileReader(awsCredentialsFile));
                accessKey = p.getProperty(PUBLIC_ACCESS_KEY_ID);
                if (!validateAccessKey(accessKey)) {
                    System.out.println("The AWS Acess key \"" + accessKey + "\" provided by you is invalid. It should be 20 character string.");
                    System.exit(-2);
                }
                secretKey = p.getProperty(SECRET_ACCESS_KEY);
                if (!validateSecretKey(secretKey)) {
                    System.out.println("The AWS Secret key \"" + secretKey + "\" provided by you is invalid. It should be 40 character string.");
                    System.exit(-2);
                }
            } catch (IOException ioexp) {
                System.err.println("Unable to read AWS Credentials file " + awsCredentialsFile);
                ioexp.printStackTrace();
                System.exit(-2);
            }
        }

         BasicAWSCredentials credentials = new BasicAWSCredentials(accessKey, secretKey);
         AmazonEC2 ec2Client = new AmazonEC2Client(credentials);
         return ec2Client;
    }

     /**
     * Validate whether the AWS public Access Key Id is valid
     *
     * @param key to check
     * @return true if valid and false if not
     */
    public static boolean validateAccessKey(String key) {
        if (key == null || key.length() != 20)
            return false;
        return true;
    }

    /**
     * Validate whether the AWS secret access key is valid
     *
     * @param key to check
     * @return true if valid and false if not
     */
     public static boolean validateSecretKey(String key) {
        if (key == null || key.length() != 40)
            return false;
        return true;
    }

     /**
      * Save the AMI names into the file
      *
      * @throws FileNotFoundException - unable to create File specified by this.signatureFilename
      * @throws IOException - problem writing to the File specified by this.signatureFilename
      */
      public static void writeAmisToFile(List<Image> amiList) {
        File file = null;
        FileWriter fw = null;
        BufferedWriter br = null;
        try {
            file = new File(AmazonAmi.AMI_FILE_NAME);
            fw = new FileWriter(file);
            br = new BufferedWriter(fw);
            br.write("*********************************************");
            br.write(System.getProperty(LINE_SEPARATOR));
            br.write("AMAZON MACHINE IMAGES - SYDNEY REGION");
            br.write(System.getProperty(LINE_SEPARATOR));
            br.write("*********************************************");
            br.write(System.getProperty(LINE_SEPARATOR));
            for (Image image : amiList) {
                br.write("AMI Id : " + image.getImageId() + "\t");
                br.write("AMI Name : " + image.getName() + "\t");
                br.write("AMI Location : " + image.getImageLocation() + "\t");
                br.write(System.getProperty(LINE_SEPARATOR));
            }
            br.flush();
        }catch (FileNotFoundException exp) {
            exp.printStackTrace();
        } catch (IOException ioexp) {
            ioexp.printStackTrace();
        }  finally {
            try {
                br.close();
                fw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * Main method to start
     * @param args
     */
    public static void main(String[] args) {
        AmazonAmi.getAmazonMachineImages();
    }
}




Aucun commentaire:

Enregistrer un commentaire