Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package Aws; import com.amazonaws.auth.profile.ProfileCredentialsProvider; import com.amazonaws.auth.profile.ProfilesConfigFile; import com.amazonaws.services.ec2.AmazonEC2Client; import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.Map; import java.util.Scanner; import java.util.logging.Level; import java.util.logging.Logger; /** * * @author ema */ public class Profile { String clientname = null; String region = null; public Profile() { } public ProfileCredentialsProvider LoadCredentials() { //Load credentials from file File configfile = new File(System.getProperty("user.home"), ".aws/credentials"); ProfilesConfigFile credentials = new ProfilesConfigFile(configfile); ArrayList CustomerList = getCustomerList(credentials); String profile = ProfileChoice(CustomerList); int client = CustomerList.indexOf(profile); ProfileCredentialsProvider provider = null; do { try { provider = new ProfileCredentialsProvider(credentials, CustomerList.get(client).toString()); } catch (ArrayIndexOutOfBoundsException ev) { System.out.println("No Profile present for the customer selected"); profile = ProfileChoice(CustomerList); client = CustomerList.indexOf(profile); provider = new ProfileCredentialsProvider(credentials, CustomerList.get(client).toString()); } } while (!profile.equals(CustomerList.get(client).toString())); return provider; } public String ProfileChoice(ArrayList customers) { //As you may have many profiles to chose from , let's let the user pick one System.out.println("Which profile do you want to use?"); System.out.println(customers.toString()); Scanner scan = new Scanner(System.in); String profile = scan.next(); setClientname(profile); return profile; } public ArrayList getCustomerList(ProfilesConfigFile credentials) { //As you may have many profiles let's retrive them all ArrayList customers = new ArrayList(); for (String key : credentials.getAllProfiles().keySet()) { customers.add(key); } return customers; } public Map LoadContent() { File configfile = new File(System.getProperty("user.home"), ".aws/config"); BufferedReader reader = null; String line = null; Map text = new HashMap<>(); ArrayList key = new ArrayList(); ArrayList value = new ArrayList(); try { reader = new BufferedReader(new FileReader(configfile)); while ((line = reader.readLine()) != null) { if (line.startsWith("[")) { key.add(line); } else if (line.startsWith("region")) { String[] split = line.split("="); value.add(split[1]); } } for (int i = 0; i < value.size(); i++) { text.put(key.get(i), value.get(i)); } } catch (FileNotFoundException ex) { Logger.getLogger(AWS.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(AWS.class.getName()).log(Level.SEVERE, null, ex); } return text; } public void SetEndEndpoint(AmazonEC2Client ec2, Map<String, String> region) { String key = getClientname(); if (region.toString().contains(key)) { ec2.setEndpoint("ec2." + region.get(key).trim() + ".amazonaws.com"); } } public void setClientname(String clientname) { if ("default".equals(clientname)) { this.clientname = "[" + clientname + "]"; } else this.clientname = "[profile " + clientname + "]"; } public String getClientname() { return clientname; } }