List of usage examples for com.amazonaws AmazonServiceException getMessage
@Override
public String getMessage()
From source file:controllers.s3locationmodify.java
License:Open Source License
public static void main(String[] args) throws Exception { /*/*from w ww . j a v a 2 s . c o m*/ * The ProfileCredentialsProvider will return your [default] * credential profile by reading from the credentials file located at * (/home/sravya/.aws/credentials). * * TransferManager manages a pool of threads, so we create a * single instance and share it throughout our application. */ try { credentials = new ProfileCredentialsProvider("default").getCredentials(); } catch (Exception e) { throw new AmazonClientException("Cannot load the credentials from the credential profiles file. " + "Please make sure that your credentials file is at the correct " + "location (/home/sravya/.aws/credentials), and is in valid format.", e); } int argLen = args.length; Region reg = Region.getRegion(Regions.US_WEST_2); int hack = 0; int userrequests; try { userrequests = Integer.parseInt(args[argLen - 1]); } catch (NumberFormatException e) { userrequests = 1; String use = args[argLen - 1]; if (use.equals("Australia")) { hack = 0; } else if (use.equals("SouthAfrica")) { hack = 1; } else if (use.equals("India")) { hack = 2; } else if (use.equals("UnitedKingdom")) { hack = 3; } else if (use.equals("China")) { hack = 4; } else if (use.equals("Germany")) { hack = 5; } else if (use.equals("France")) { hack = 6; } else if (use.equals("Japan")) { hack = 7; } else if (use.equals("Thailand")) { hack = 8; } else if (use.equals("Spain")) { hack = 9; } } int filecount = 0; for (int m = 0; m < argLen - 1; m++) { filecount = filecount + 1; } int numphotos = filecount; int numIDCs = numphotos; int locationInd = 0; long[] cusize = new long[6]; long photosspace = 0; for (int mm = 0; mm < userrequests; mm++) { for (int i = 0; i < cusize.length; i++) cusize[i] = 0; ArrayList<Integer> originalgarph = new ArrayList<Integer>(); loadObj ob = calculateload(); long[] regionload = new long[5]; regionload = ob.load; for (int i = 0; i < regionload.length; i++) { if (regionload[i] == 0) { regionload[i] = 1000; } } /*for (int i=0; i<5 ;i++) { System.out.println(regionload[i]); double diffload=0; double avgload=0; int count=0; for(int j=0;j<5; j++) { if(j!=i) { avgload = avgload + regionload[j]; count++; } } avgload= (avgload / count); diffload= (regionload[i]/avgload) ; System.out.println("avgload: "+ avgload); System.out.println("diffload: "+ diffload); if(diffload < 1.8) { originalgarph.add(i+1); } }*/ for (int i = 0; i < 5; i++) { originalgarph.add(i + 1); } availSpaceNorthCal = maxsize - regionload[0]; photosspace = numphotos * 6000; if (availSpaceNorthCal < photosspace) { availSpaceNorthCal = maxsize; cusize[1] = availSpaceNorthCal / 6000; } else { cusize[1] = availSpaceNorthCal / 6000; } availSpaceOregon = maxsize - regionload[1]; photosspace = numphotos * 6000; if (availSpaceOregon < photosspace) { availSpaceOregon = maxsize; cusize[2] = availSpaceOregon / 6000; } else { cusize[2] = availSpaceOregon / 6000; } availSpaceSingapore = maxsize - regionload[2]; photosspace = numphotos * 6000; if (availSpaceSingapore < photosspace) { availSpaceSingapore = maxsize; cusize[3] = availSpaceSingapore / 6000; } else { cusize[3] = availSpaceSingapore / 6000; } availSpaceTokyo = maxsize - regionload[3]; photosspace = numphotos * 6000; if (availSpaceTokyo < photosspace) { availSpaceTokyo = maxsize; cusize[4] = availSpaceTokyo / 6000; } else { cusize[4] = availSpaceTokyo / 6000; } availSpaceSydney = maxsize - regionload[4]; photosspace = numphotos * 6000; if (availSpaceSydney < photosspace) { availSpaceSydney = maxsize; cusize[5] = availSpaceSydney / 6000; } else { cusize[5] = availSpaceSydney / 6000; } int cou = originalgarph.size(); String fileName = algorithmPath + "request.alg"; PrintWriter writer = new PrintWriter(fileName, "UTF-8"); for (int i = 0; i < cou; i++) { int value = originalgarph.get(i); writer.print(value + " "); System.out.println(" IDC " + String.valueOf(value)); } writer.println(); for (int i = 0; i < originalgarph.size(); i++) { int j = originalgarph.get(i); writer.print(cusize[j] + " "); } writer.println(); writer.println(1); if (userrequests != 1) { locationInd = randInt(0, 9); } else { locationInd = hack; } writer.println(locationInd); System.out.println(" locationInd " + String.valueOf(locationInd)); writer.println(numphotos); System.out.println(" numphotos " + String.valueOf(numphotos)); writer.println(numIDCs); System.out.println(" numIDCs " + String.valueOf(numIDCs)); writer.close(); originalgarph.clear(); try { String prg = "import sys\nprint int(sys.argv[1])+int(sys.argv[2])\n"; String pythonCmd = "/usr/bin/python " + algorithmPath + "ramd.py"; Process p = Runtime.getRuntime().exec(pythonCmd); try { Thread.sleep(2000); //1000 milliseconds is one second. } catch (InterruptedException ex) { Thread.currentThread().interrupt(); } String fileName1 = algorithmPath + "work.alg"; File log = new File(fileName1); int filenumber = 0; String fileName2 = algorithmPath + "filenumber.alg"; Scanner numberscan = new Scanner(new File(fileName2)); if (numberscan.hasNextLine()) { filenumber = numberscan.nextInt(); } else { filenumber = 1; } numberscan.close(); ArrayList<String> photofnames = new ArrayList<String>(); ArrayList<String> argFNames = new ArrayList<String>(); for (int ll = 0; ll < argLen - 1; ll++) { photofnames.add(photosPath + args[ll] + ".bmp"); argFNames.add(args[ll]); System.out.println("Will upload " + photosPath + args[ll] + ".bmp"); } String sCurrentLine; BufferedReader br = null; br = new BufferedReader(new FileReader(fileName1)); int currLine = 0; Integer userNumber = 0; ArrayList<Integer> idcSet = new ArrayList<Integer>(); ArrayList<Integer> photonos = new ArrayList<Integer>(); int currU = 0; while ((sCurrentLine = br.readLine()) != null) { if (currLine % 3 == 0) { userNumber = Integer.parseInt(sCurrentLine); idcSet.clear(); photonos.clear(); } if (currLine % 3 == 1) { String[] idcnums = sCurrentLine.split(" "); String regions = ""; String regionsFileName = algorithmPath + "regions.alg"; PrintWriter regionwriter = new PrintWriter(regionsFileName, "UTF-8"); for (int numIdcs = 0; numIdcs < idcnums.length; numIdcs++) { idcSet.add(Integer.parseInt(idcnums[numIdcs])); if (idcnums[numIdcs].equals("1")) { regions = "region1"; regionwriter.print(regions + " "); } else if (idcnums[numIdcs].equals("2")) { regions = "region2"; regionwriter.print(regions + " "); } else if (idcnums[numIdcs].equals("3")) { regions = "region3"; regionwriter.print(regions + " "); } else if (idcnums[numIdcs].equals("4")) { regions = "region4"; regionwriter.print(regions + " "); } else if (idcnums[numIdcs].equals("5")) { regions = "region5"; regionwriter.print(regions + " "); } System.out.println("IDCs: " + idcnums[numIdcs]); } regionwriter.close(); } if (currLine % 3 == 2) { String[] idcpnums = sCurrentLine.split(" "); for (int numIdcs = 0; numIdcs < idcpnums.length; numIdcs++) { photonos.add(Integer.parseInt(idcpnums[numIdcs])); } ArrayList<String> smallestBnames = new ArrayList<String>(); ArrayList<String> bucketnames = new ArrayList<String>(); for (int tot = 0; tot < idcSet.size(); tot++) { smallestBnames.add(ob.bnames[idcSet.get(tot) - 1]); } AmazonS3 s3client = new AmazonS3Client(new ProfileCredentialsProvider()); int currPno = 0; ArrayList<String> transferThese = new ArrayList<String>(); ArrayList<Integer> transferThesebno = new ArrayList<Integer>(); System.out.println(String.valueOf(idcpnums.length)); //upload everything to 1 bucket for (int numIdcs = 0; numIdcs < idcpnums.length; numIdcs++) { for (int numP = 0; numP < photonos.get(numIdcs); numP++) { String uploadFileName = photofnames.get(currPno); String keyName = String.valueOf(userNumber) + "_" + argFNames.get(currPno) + '_' + filenumber++ + ".bmp"; if (numIdcs > 0) { transferThese.add(keyName); transferThesebno.add(numIdcs); } try { System.out.println("Uploading " + uploadFileName + " to " + smallestBnames.get(0) + " with keyname " + keyName); File file = new File(uploadFileName); s3client.putObject(new PutObjectRequest(smallestBnames.get(0), keyName, file)); } catch (AmazonServiceException ase) { System.out.println("Caught an AmazonServiceException, which " + "means your request made it " + "to Amazon S3, but was rejected with an error response" + " for some reason."); System.out.println("Error Message: " + ase.getMessage()); System.out.println("HTTP Status Code: " + ase.getStatusCode()); System.out.println("AWS Error Code: " + ase.getErrorCode()); System.out.println("Error Type: " + ase.getErrorType()); System.out.println("Request ID: " + ase.getRequestId()); } currPno++; } } //transfer other files System.out.println("Number of files to transfer " + String.valueOf(transferThese.size())); for (int tot = 0; tot < transferThese.size(); tot++) { String source = smallestBnames.get(0); String dest = smallestBnames.get(transferThesebno.get(tot)); String fname = transferThese.get(tot); String src = "s3://" + source + "/" + fname; String d = "s3://" + dest; String cmd = "aws s3 mv " + src + " " + d + "\n"; System.out.println("Moving " + src + " to " + d + "\n"); Process p1 = Runtime.getRuntime().exec(cmd); } currU++; if (currU >= 1) { transferThese.clear(); transferThesebno.clear(); photofnames.clear(); argFNames.clear(); smallestBnames.clear(); break; } } currLine++; } String fileNumberFilePath = algorithmPath + "filenumber.alg"; PrintWriter numberwriter = new PrintWriter(fileNumberFilePath, "UTF-8"); numberwriter.println(filenumber); numberwriter.close(); } catch (Exception e) { } } // String s = null; // Process p1 = Runtime.getRuntime().exec("ls -alrt"); // // BufferedReader stdInput = new BufferedReader(new // InputStreamReader(p1.getInputStream())); // // BufferedReader stdError = new BufferedReader(new // InputStreamReader(p1.getErrorStream())); // // // read the output from the command // System.out.println("Here is the standard output of the command:\n"); // while ((s = stdInput.readLine()) != null) { // System.out.println(s); // } }
From source file:controllers.s3modify.java
License:Open Source License
public static void main(String[] args) throws Exception { /*/*from ww w .ja v a2 s . c om*/ * The ProfileCredentialsProvider will return your [default] * credential profile by reading from the credentials file located at * (/home/sravya/.aws/credentials). * * TransferManager manages a pool of threads, so we create a * single instance and share it throughout our application. */ try { credentials = new ProfileCredentialsProvider("default").getCredentials(); } catch (Exception e) { throw new AmazonClientException("Cannot load the credentials from the credential profiles file. " + "Please make sure that your credentials file is at the correct " + "location (/home/sravya/.aws/credentials), and is in valid format.", e); } int argLen = args.length; Region reg = Region.getRegion(Regions.US_WEST_2); int hack = 0; int userrequests; try { userrequests = Integer.parseInt(args[argLen - 1]); } catch (NumberFormatException e) { userrequests = 1; String use = args[argLen - 1]; if (use.equals("Australia")) { hack = 0; } else if (use.equals("SouthAfrica")) { hack = 1; } else if (use.equals("India")) { hack = 2; } else if (use.equals("UnitedKingdom")) { hack = 3; } else if (use.equals("China")) { hack = 4; } else if (use.equals("Germany")) { hack = 5; } else if (use.equals("France")) { hack = 6; } else if (use.equals("Japan")) { hack = 7; } else if (use.equals("Thailand")) { hack = 8; } else if (use.equals("Spain")) { hack = 9; } } int filecount = 0; for (int m = 0; m < argLen - 1; m++) { filecount = filecount + 1; } int numphotos = filecount; int numIDCs = numphotos; int locationInd = 0; long[] cusize = new long[6]; long photosspace = 0; for (int mm = 0; mm < userrequests; mm++) { for (int i = 0; i < cusize.length; i++) cusize[i] = 0; ArrayList<Integer> originalgarph = new ArrayList<Integer>(); loadObj ob = calculateload(); long[] regionload = new long[5]; regionload = ob.load; for (int i = 0; i < regionload.length; i++) { if (regionload[i] == 0) { regionload[i] = 1000; } } for (int i = 0; i < 5; i++) { System.out.println(regionload[i]); double diffload = 0; double avgload = 0; int count = 0; for (int j = 0; j < 5; j++) { if (j != i) { avgload = avgload + regionload[j]; count++; } } avgload = (avgload / count); diffload = (regionload[i] / avgload); System.out.println("avgload: " + avgload); System.out.println("diffload: " + diffload); if (diffload < 1.8) { originalgarph.add(i + 1); } } availSpaceNorthCal = maxsize - regionload[0]; photosspace = numphotos * 6000; if (availSpaceNorthCal < photosspace) { availSpaceNorthCal = maxsize; cusize[1] = availSpaceNorthCal / 6000; } else { cusize[1] = availSpaceNorthCal / 6000; } availSpaceOregon = maxsize - regionload[1]; photosspace = numphotos * 6000; if (availSpaceOregon < photosspace) { availSpaceOregon = maxsize; cusize[2] = availSpaceOregon / 6000; } else { cusize[2] = availSpaceOregon / 6000; } availSpaceSingapore = maxsize - regionload[2]; photosspace = numphotos * 6000; if (availSpaceSingapore < photosspace) { availSpaceSingapore = maxsize; cusize[3] = availSpaceSingapore / 6000; } else { cusize[3] = availSpaceSingapore / 6000; } availSpaceTokyo = maxsize - regionload[3]; photosspace = numphotos * 6000; if (availSpaceTokyo < photosspace) { availSpaceTokyo = maxsize; cusize[4] = availSpaceTokyo / 6000; } else { cusize[4] = availSpaceTokyo / 6000; } availSpaceSydney = maxsize - regionload[4]; photosspace = numphotos * 6000; if (availSpaceSydney < photosspace) { availSpaceSydney = maxsize; cusize[5] = availSpaceSydney / 6000; } else { cusize[5] = availSpaceSydney / 6000; } int cou = originalgarph.size(); String fileName = algorithmPath + "request.alg"; PrintWriter writer = new PrintWriter(fileName, "UTF-8"); for (int i = 0; i < cou; i++) { int value = originalgarph.get(i); writer.print(value + " "); System.out.println(" IDC " + String.valueOf(value)); } writer.println(); for (int i = 0; i < originalgarph.size(); i++) { int j = originalgarph.get(i); writer.print(cusize[j] + " "); } writer.println(); writer.println(1); if (userrequests != 1) { locationInd = randInt(0, 9); } else { locationInd = hack; } writer.println(locationInd); System.out.println(" locationInd " + String.valueOf(locationInd)); writer.println(numphotos); System.out.println(" numphotos " + String.valueOf(numphotos)); writer.println(numIDCs); System.out.println(" numIDCs " + String.valueOf(numIDCs)); writer.close(); originalgarph.clear(); try { String prg = "import sys\nprint int(sys.argv[1])+int(sys.argv[2])\n"; String pythonCmd = "/usr/bin/python " + algorithmPath + "ramd.py"; Process p = Runtime.getRuntime().exec(pythonCmd); try { Thread.sleep(2000); //1000 milliseconds is one second. } catch (InterruptedException ex) { Thread.currentThread().interrupt(); } String fileName1 = algorithmPath + "work.alg"; File log = new File(fileName1); int filenumber = 0; String fileName2 = algorithmPath + "filenumber.alg"; Scanner numberscan = new Scanner(new File(fileName2)); if (numberscan.hasNextLine()) { filenumber = numberscan.nextInt(); } else { filenumber = 1; } numberscan.close(); ArrayList<String> photofnames = new ArrayList<String>(); ArrayList<String> argFNames = new ArrayList<String>(); for (int ll = 0; ll < argLen - 1; ll++) { photofnames.add(photosPath + args[ll] + ".bmp"); argFNames.add(args[ll]); System.out.println("Will upload " + photosPath + args[ll] + ".bmp"); } String sCurrentLine; BufferedReader br = null; br = new BufferedReader(new FileReader(fileName1)); int currLine = 0; Integer userNumber = 0; ArrayList<Integer> idcSet = new ArrayList<Integer>(); ArrayList<Integer> photonos = new ArrayList<Integer>(); int currU = 0; while ((sCurrentLine = br.readLine()) != null) { if (currLine % 3 == 0) { userNumber = Integer.parseInt(sCurrentLine); idcSet.clear(); photonos.clear(); } if (currLine % 3 == 1) { String[] idcnums = sCurrentLine.split(" "); String regions = ""; String regionsFileName = algorithmPath + "regions.alg"; PrintWriter regionwriter = new PrintWriter(regionsFileName, "UTF-8"); for (int numIdcs = 0; numIdcs < idcnums.length; numIdcs++) { idcSet.add(Integer.parseInt(idcnums[numIdcs])); if (idcnums[numIdcs].equals("1")) { regions = "region1"; regionwriter.print(regions + " "); } else if (idcnums[numIdcs].equals("2")) { regions = "region2"; regionwriter.print(regions + " "); } else if (idcnums[numIdcs].equals("3")) { regions = "region3"; regionwriter.print(regions + " "); } else if (idcnums[numIdcs].equals("4")) { regions = "region4"; regionwriter.print(regions + " "); } else if (idcnums[numIdcs].equals("5")) { regions = "region5"; regionwriter.print(regions + " "); } System.out.println("IDCs: " + idcnums[numIdcs]); } regionwriter.close(); } if (currLine % 3 == 2) { String[] idcpnums = sCurrentLine.split(" "); for (int numIdcs = 0; numIdcs < idcpnums.length; numIdcs++) { photonos.add(Integer.parseInt(idcpnums[numIdcs])); } ArrayList<String> smallestBnames = new ArrayList<String>(); ArrayList<String> bucketnames = new ArrayList<String>(); for (int tot = 0; tot < idcSet.size(); tot++) { smallestBnames.add(ob.bnames[idcSet.get(tot) - 1]); } AmazonS3 s3client = new AmazonS3Client(new ProfileCredentialsProvider()); int currPno = 0; ArrayList<String> transferThese = new ArrayList<String>(); ArrayList<Integer> transferThesebno = new ArrayList<Integer>(); System.out.println(String.valueOf(idcpnums.length)); //upload everything to 1 bucket for (int numIdcs = 0; numIdcs < idcpnums.length; numIdcs++) { for (int numP = 0; numP < photonos.get(numIdcs); numP++) { String uploadFileName = photofnames.get(currPno); String keyName = String.valueOf(userNumber) + "_" + argFNames.get(currPno) + '_' + filenumber++ + ".bmp"; if (numIdcs > 0) { transferThese.add(keyName); transferThesebno.add(numIdcs); } try { System.out.println("Uploading " + uploadFileName + " to " + smallestBnames.get(0) + " with keyname " + keyName); File file = new File(uploadFileName); s3client.putObject(new PutObjectRequest(smallestBnames.get(0), keyName, file)); } catch (AmazonServiceException ase) { System.out.println("Caught an AmazonServiceException, which " + "means your request made it " + "to Amazon S3, but was rejected with an error response" + " for some reason."); System.out.println("Error Message: " + ase.getMessage()); System.out.println("HTTP Status Code: " + ase.getStatusCode()); System.out.println("AWS Error Code: " + ase.getErrorCode()); System.out.println("Error Type: " + ase.getErrorType()); System.out.println("Request ID: " + ase.getRequestId()); } currPno++; } } //transfer other files System.out.println("Number of files to transfer " + String.valueOf(transferThese.size())); for (int tot = 0; tot < transferThese.size(); tot++) { String source = smallestBnames.get(0); String dest = smallestBnames.get(transferThesebno.get(tot)); String fname = transferThese.get(tot); String src = "s3://" + source + "/" + fname; String d = "s3://" + dest; String cmd = "aws s3 mv " + src + " " + d + "\n"; System.out.println("Moving " + src + " to " + d + "\n"); Process p1 = Runtime.getRuntime().exec(cmd); } currU++; if (currU >= 1) { transferThese.clear(); transferThesebno.clear(); photofnames.clear(); argFNames.clear(); smallestBnames.clear(); break; } } currLine++; } String fileNumberFilePath = algorithmPath + "filenumber.alg"; PrintWriter numberwriter = new PrintWriter(fileNumberFilePath, "UTF-8"); numberwriter.println(filenumber); numberwriter.close(); } catch (Exception e) { } } // String s = null; // Process p1 = Runtime.getRuntime().exec("ls -alrt"); // // BufferedReader stdInput = new BufferedReader(new // InputStreamReader(p1.getInputStream())); // // BufferedReader stdError = new BufferedReader(new // InputStreamReader(p1.getErrorStream())); // // // read the output from the command // System.out.println("Here is the standard output of the command:\n"); // while ((s = stdInput.readLine()) != null) { // System.out.println(s); // } }
From source file:cs.iit.edu.ckmr.worker.MonitorTableCreator.java
License:Open Source License
public static void main(String[] args) throws Exception { init();//from w ww .ja v a2s .c o m long readCapacity = 2L; long writeCapacity = 2L; try { String tableName = "monitor"; // DeleteTableRequest deleteTableRequest = new // DeleteTableRequest(tableName); // dynamoDB.deleteTable(deleteTableRequest); // // waitForTableToBecomeAvailable(tableName); // Create a table with a primary hash key named 'name', which holds // a string CreateTableRequest createTableRequest = new CreateTableRequest().withTableName(tableName) .withKeySchema(new KeySchemaElement().withAttributeName("id").withKeyType(KeyType.HASH)) .withAttributeDefinitions(new AttributeDefinition().withAttributeName("id") .withAttributeType(ScalarAttributeType.N)) .withProvisionedThroughput(new ProvisionedThroughput().withReadCapacityUnits(readCapacity) .withWriteCapacityUnits(writeCapacity)); TableDescription createdTableDescription = dynamoDB.createTable(createTableRequest) .getTableDescription(); System.out.println("Created Table: " + createdTableDescription); // Wait for it to become active waitForTableToBecomeAvailable(tableName); // Describe our new table DescribeTableRequest describeTableRequest = new DescribeTableRequest().withTableName(tableName); TableDescription tableDescription = dynamoDB.describeTable(describeTableRequest).getTable(); System.out.println("Table Description: " + tableDescription); } catch (AmazonServiceException ase) { System.out.println("Caught an AmazonServiceException, which means your request made it " + "to AWS, but was rejected with an error response for some reason."); System.out.println("Error Message: " + ase.getMessage()); System.out.println("HTTP Status Code: " + ase.getStatusCode()); System.out.println("AWS Error Code: " + ase.getErrorCode()); System.out.println("Error Type: " + ase.getErrorType()); System.out.println("Request ID: " + ase.getRequestId()); } catch (AmazonClientException ace) { System.out.println("Caught an AmazonClientException, which means the client encountered " + "a serious internal problem while trying to communicate with AWS, " + "such as not being able to access the network."); System.out.println("Error Message: " + ace.getMessage()); } }
From source file:cs.iit.edu.ckmr.worker.TableCreator.java
License:Open Source License
public void initializeTable(long readCapacity, long writeCapacity) { try {// w w w .j av a 2s . c o m String tableName = "messages"; ListTablesResult listTableResult = new ListTablesResult(); listTableResult = dynamoDB.listTables(); List<String> tableNames = listTableResult.getTableNames(); if (!tableNames.contains(tableName)) { createTable(readCapacity, writeCapacity, tableName); } } catch (AmazonServiceException ase) { System.out.println("Caught an AmazonServiceException, which means your request made it " + "to AWS, but was rejected with an error response for some reason."); System.out.println("Error Message: " + ase.getMessage()); System.out.println("HTTP Status Code: " + ase.getStatusCode()); System.out.println("AWS Error Code: " + ase.getErrorCode()); System.out.println("Error Type: " + ase.getErrorType()); System.out.println("Request ID: " + ase.getRequestId()); } catch (AmazonClientException ace) { System.out.println("Caught an AmazonClientException, which means the client encountered " + "a serious internal problem while trying to communicate with AWS, " + "such as not being able to access the network."); System.out.println("Error Message: " + ace.getMessage()); } }
From source file:Database.CustomerData.java
License:Open Source License
public static void main(String[] args) throws Exception { init();//w w w . ja v a2 s. c om try { // Create table if it does not exist yet if (Tables.doesTableExist(dynamoDB, tableName)) { System.out.println("Table " + tableName + " is already ACTIVE"); } else { // Create a table with a primary hash key Idd 'Id', which holds a string CreateTableRequest createTableRequest = new CreateTableRequest().withTableName(tableName) .withKeySchema(new KeySchemaElement().withAttributeName("Id").withKeyType(KeyType.HASH)) .withAttributeDefinitions(new AttributeDefinition().withAttributeName("Id") .withAttributeType(ScalarAttributeType.S)) .withProvisionedThroughput( new ProvisionedThroughput().withReadCapacityUnits(1L).withWriteCapacityUnits(1L)); TableDescription createdTableDescription = dynamoDB.createTable(createTableRequest) .getTableDescription(); System.out.println("Created Table: " + createdTableDescription); // Wait for it to become active System.out.println("Waiting for " + tableName + " to become ACTIVE..."); Tables.waitForTableToBecomeActive(dynamoDB, tableName); // Describe our new table DescribeTableRequest describeTableRequest = new DescribeTableRequest().withTableName(tableName); TableDescription tableDescription = dynamoDB.describeTable(describeTableRequest).getTable(); System.out.println("Table Description: " + tableDescription); // Add an item Map<String, AttributeValue> item = newItem("Nine Creatives on Creativity", "SQ_NS001", 2014, "Squashouse", "Video"); PutItemRequest putItemRequest = new PutItemRequest(tableName, item); PutItemResult putItemResult = dynamoDB.putItem(putItemRequest); } } catch (AmazonServiceException ase) { System.out.println("Caught an AmazonServiceException, which means your request made it " + "to AWS, but was rejected with an error response for some reason."); System.out.println("Error Message: " + ase.getMessage()); System.out.println("HTTP Status Code: " + ase.getStatusCode()); System.out.println("AWS Error Code: " + ase.getErrorCode()); System.out.println("Error Type: " + ase.getErrorType()); System.out.println("Request ID: " + ase.getRequestId()); } catch (AmazonClientException ace) { System.out.println("Caught an AmazonClientException, which means the client encountered " + "a serious internal problem while trying to communicate with AWS, " + "such as not being able to access the network."); System.out.println("Error Message: " + ace.getMessage()); } }
From source file:Database.ProductData.java
License:Open Source License
public static void main(String[] args) throws Exception { init();/*from www . j ava2 s .co m*/ try { String tableName = "CustomerData"; // Create table if it does not exist yet if (Tables.doesTableExist(dynamoDB, tableName)) { System.out.println("Table " + tableName + " is already ACTIVE"); } else { // Create a table with a primary hash key named 'name', which holds a string CreateTableRequest createTableRequest = new CreateTableRequest().withTableName(tableName) .withKeySchema(new KeySchemaElement().withAttributeName("name").withKeyType(KeyType.HASH)) .withAttributeDefinitions(new AttributeDefinition().withAttributeName("name") .withAttributeType(ScalarAttributeType.S)) .withProvisionedThroughput( new ProvisionedThroughput().withReadCapacityUnits(1L).withWriteCapacityUnits(1L)); TableDescription createdTableDescription = dynamoDB.createTable(createTableRequest) .getTableDescription(); System.out.println("Created Table: " + createdTableDescription); // Wait for it to become active System.out.println("Waiting for " + tableName + " to become ACTIVE..."); Tables.waitForTableToBecomeActive(dynamoDB, tableName); } // Describe our new table DescribeTableRequest describeTableRequest = new DescribeTableRequest().withTableName(tableName); TableDescription tableDescription = dynamoDB.describeTable(describeTableRequest).getTable(); System.out.println("Table Description: " + tableDescription); // Add an item Map<String, AttributeValue> item = newItem("Nine Creatives on Creativity", "SQ_NS001", 2014, "Squashouse", "Video"); PutItemRequest putItemRequest = new PutItemRequest(tableName, item); PutItemResult putItemResult = dynamoDB.putItem(putItemRequest); // System.out.println("Result: " + putItemResult); uploadSampleProducts(tableName); // System.out.println("Result: " + aQueryResult.withItems(item)); // Scan items for movies with a year attribute greater than 1985 HashMap<String, Condition> scanFilter = new HashMap<String, Condition>(); Condition condition = new Condition().withComparisonOperator(ComparisonOperator.GT.toString()) .withAttributeValueList(new AttributeValue().withN("1985")); scanFilter.put("year", condition); ScanRequest scanRequest = new ScanRequest(tableName).withScanFilter(scanFilter); ScanResult scanResult = dynamoDB.scan(scanRequest); System.out.println("Result: " + scanResult); } catch (AmazonServiceException ase) { System.out.println("Caught an AmazonServiceException, which means your request made it " + "to AWS, but was rejected with an error response for some reason."); System.out.println("Error Message: " + ase.getMessage()); System.out.println("HTTP Status Code: " + ase.getStatusCode()); System.out.println("AWS Error Code: " + ase.getErrorCode()); System.out.println("Error Type: " + ase.getErrorType()); System.out.println("Request ID: " + ase.getRequestId()); } catch (AmazonClientException ace) { System.out.println("Caught an AmazonClientException, which means the client encountered " + "a serious internal problem while trying to communicate with AWS, " + "such as not being able to access the network."); System.out.println("Error Message: " + ace.getMessage()); } }
From source file:dataMappers.PictureDataMapper.java
public static void addPictureToReport(DBConnector dbconnector, HttpServletRequest request) throws FileUploadException, IOException, SQLException { if (!ServletFileUpload.isMultipartContent(request)) { System.out.println("Invalid upload request"); return;/*ww w.j a v a2 s . c o m*/ } // Define limits for disk item DiskFileItemFactory factory = new DiskFileItemFactory(); factory.setSizeThreshold(THRESHOLD_SIZE); // Define limit for servlet upload ServletFileUpload upload = new ServletFileUpload(factory); upload.setFileSizeMax(MAX_FILE_SIZE); upload.setSizeMax(MAX_REQUEST_SIZE); FileItem itemFile = null; int reportID = 0; // Get list of items in request (parameters, files etc.) List formItems = upload.parseRequest(request); Iterator iter = formItems.iterator(); // Loop items while (iter.hasNext()) { FileItem item = (FileItem) iter.next(); if (!item.isFormField()) { itemFile = item; // If not form field, must be item } else if (item.getFieldName().equalsIgnoreCase("reportID")) { // else it is a form field try { System.out.println(item.getString()); reportID = Integer.parseInt(item.getString()); } catch (NumberFormatException e) { reportID = 0; } } } // This will be null if no fields were declared as image/upload. // Also, reportID must be > 0 if (itemFile != null || reportID == 0) { try { // Create credentials from final vars BasicAWSCredentials awsCredentials = new BasicAWSCredentials(AMAZON_ACCESS_KEY, AMAZON_SECRET_KEY); // Create client with credentials AmazonS3 s3client = new AmazonS3Client(awsCredentials); // Set region s3client.setRegion(Region.getRegion(Regions.EU_WEST_1)); // Set content length (size) of file ObjectMetadata om = new ObjectMetadata(); om.setContentLength(itemFile.getSize()); // Get extension for file String ext = FilenameUtils.getExtension(itemFile.getName()); // Generate random filename String keyName = UUID.randomUUID().toString() + '.' + ext; // This is the actual upload command s3client.putObject(new PutObjectRequest(S3_BUCKET_NAME, keyName, itemFile.getInputStream(), om)); // Picture was uploaded to S3 if we made it this far. Now we insert the row into the database for the report. PreparedStatement stmt = dbconnector.getCon() .prepareStatement("INSERT INTO reports_pictures" + "(REPORTID, PICTURE) VALUES (?,?)"); stmt.setInt(1, reportID); stmt.setString(2, keyName); stmt.executeUpdate(); stmt.close(); } catch (AmazonServiceException ase) { System.out.println("Caught an AmazonServiceException, which " + "means your request made it " + "to Amazon S3, but was rejected with an error response" + " for some reason."); System.out.println("Error Message: " + ase.getMessage()); System.out.println("HTTP Status Code: " + ase.getStatusCode()); System.out.println("AWS Error Code: " + ase.getErrorCode()); System.out.println("Error Type: " + ase.getErrorType()); System.out.println("Request ID: " + ase.getRequestId()); } catch (AmazonClientException ace) { System.out.println("Caught an AmazonClientException, which " + "means the client encountered " + "an internal error while trying to " + "communicate with S3, " + "such as not being able to access the network."); System.out.println("Error Message: " + ace.getMessage()); } } }
From source file:dbs.Getfroms3.java
public static void main(String[] args) throws IOException { AmazonS3 s3Client = AmazonS3ClientBuilder.standard().withRegion(Regions.AP_SOUTH_1) .withCredentials(new AWSStaticCredentialsProvider(credentials)).build(); //AmazonS3 s3Client = new AmazonS3Client(new ProfileCredentialsProvider("inhack2hire28")); try {//from w w w.j a va2 s .c o m System.out.println("Downloading an object"); S3Object s3object = s3Client.getObject(new GetObjectRequest(bucketName, key)); System.out.println("Content-Type: " + s3object.getObjectMetadata().getContentType()); displayTextInputStream(s3object.getObjectContent()); // Get a range of bytes from an object. GetObjectRequest rangeObjectRequest = new GetObjectRequest(bucketName, key); rangeObjectRequest.setRange(0, 10); S3Object objectPortion = s3Client.getObject(rangeObjectRequest); System.out.println("Printing bytes retrieved."); displayTextInputStream(objectPortion.getObjectContent()); } catch (AmazonServiceException ase) { System.out.println("Caught an AmazonServiceException, which" + " means your request made it " + "to Amazon S3, but was rejected with an error response" + " for some reason."); System.out.println("Error Message: " + ase.getMessage()); System.out.println("HTTP Status Code: " + ase.getStatusCode()); System.out.println("AWS Error Code: " + ase.getErrorCode()); System.out.println("Error Type: " + ase.getErrorType()); System.out.println("Request ID: " + ase.getRequestId()); } catch (AmazonClientException ace) { System.out.println("Caught an AmazonClientException, which means" + " the client encountered " + "an internal error while trying to " + "communicate with S3, " + "such as not being able to access the network."); System.out.println("Error Message: " + ace.getMessage()); } }
From source file:de.fischer.thotti.ec2.clients.EC2InternalClient.java
License:Apache License
/** * Handles an {@link AmazonServiceException} by logging the information * provided by Amazon AWS and turning the caught exception into * an {@link de.fischer.thotti.awscommon.AWSRemoteServiceException}. *///from w w w. j ava 2 s.c om protected void handleAmazonServiceException(AmazonServiceException exception) throws AWSRemoteServiceException { if (logger.isErrorEnabled()) { String message = exception.getMessage(); String code = exception.getErrorCode(); AmazonServiceException.ErrorType type = exception.getErrorType(); String requestId = exception.getRequestId(); String service = exception.getServiceName(); logger.error("Amazon AWS was not able to fullfil the last operation. " + "Error message = {}, error code = {}, error type = {}, " + "service = {}, requestID = {}", new Object[] { message, code, type, requestId, service }); } throw new AWSRemoteServiceException(exception.getMessage()); }
From source file:de.unibi.cebitec.bibigrid.meta.aws.CreateClusterAWS.java
@Override public boolean launchClusterInstances() { log.info("Requesting master instance ..."); if (config.isUseSpotInstances()) { RequestSpotInstancesRequest masterReq = new RequestSpotInstancesRequest(); masterReq.withType(SpotInstanceType.OneTime).withInstanceCount(1).withLaunchGroup("lg_" + clusterId) .withSpotPrice(Double.toString(config.getBidPriceMaster())); LaunchSpecification masterLaunchSpecification = new LaunchSpecification(); masterLaunchSpecification/*from w ww.j av a2 s . c o m*/ .withInstanceType(InstanceType.fromValue(this.config.getMasterInstanceType().getValue())) .withPlacement(spotInstancePlacement).withKeyName(this.config.getKeypair()) .withImageId(this.config.getMasterImage()).withUserData(base64MasterUserData) .withBlockDeviceMappings(masterDeviceMappings).withNetworkInterfaces(masterNetworkInterfaces); masterReq.setLaunchSpecification(masterLaunchSpecification); RequestSpotInstancesResult masterReqResult = ec2.requestSpotInstances(masterReq); List<SpotInstanceRequest> masterReqResponses = masterReqResult.getSpotInstanceRequests(); // collect all spotInstanceRequestIds ... List<String> spotInstanceRequestIds = new ArrayList<>(); for (SpotInstanceRequest requestResponse : masterReqResponses) { spotInstanceRequestIds.add(requestResponse.getSpotInstanceRequestId()); } // Tag spotrequest CreateTagsRequest ctr = new CreateTagsRequest(); ctr.withResources(spotInstanceRequestIds); ctr.withTags(bibigridid, username, new Tag().withKey("Name").withValue(PREFIX + "master-" + clusterId)); ec2.createTags(ctr); // Wait for spot request finished log.info("Waiting for master instance (spot request) to finish booting ..."); masterInstance = waitForInstances(waitForSpotInstances(spotInstanceRequestIds)).get(0); } else { RunInstancesRequest masterReq = new RunInstancesRequest(); masterReq.withInstanceType(InstanceType.fromValue(this.config.getMasterInstanceType().getValue())) .withMinCount(1).withMaxCount(1).withPlacement(instancePlacement) .withKeyName(this.config.getKeypair()).withImageId(this.config.getMasterImage()) .withUserData(base64MasterUserData).withBlockDeviceMappings(masterDeviceMappings) .withNetworkInterfaces(masterNetworkInterfaces); // mounting ephemerals RunInstancesResult masterReqResult = ec2.runInstances(masterReq); String masterReservationId = masterReqResult.getReservation().getReservationId(); masterInstance = masterReqResult.getReservation().getInstances().get(0); log.info("Waiting for master instance to finish booting ..."); ///////////////////////////////////////////// //// Waiting for master instance to run //// masterInstance = waitForInstances(Arrays.asList(new String[] { masterInstance.getInstanceId() })) .get(0); } log.info(I, "Master instance is now running!"); ModifyInstanceAttributeRequest ia_req = new ModifyInstanceAttributeRequest(); ia_req.setInstanceId(masterInstance.getInstanceId()); ia_req.setSourceDestCheck(Boolean.FALSE); ec2.modifyInstanceAttribute(ia_req); //////////////////////////////////// //// Tagging Master with a name //// CreateTagsRequest masterNameTagRequest = new CreateTagsRequest(); masterNameTagRequest.withResources(masterInstance.getInstanceId()).withTags(bibigridid, username, new Tag().withKey("Name").withValue(PREFIX + "master-" + clusterId)); ec2.createTags(masterNameTagRequest); /* * Waiting for Status Checks to finish * */ log.info("Waiting for Status Checks on master ..."); do { DescribeInstanceStatusRequest request = new DescribeInstanceStatusRequest(); request.setInstanceIds((Arrays.asList(new String[] { masterInstance.getInstanceId() }))); DescribeInstanceStatusResult response = ec2.describeInstanceStatus(request); InstanceStatus status = response.getInstanceStatuses().get(0); String instanceStatus = status.getInstanceStatus().getStatus(); String systemStatus = status.getSystemStatus().getStatus(); log.debug("Status of master instance: " + instanceStatus + "," + systemStatus); if (instanceStatus.equalsIgnoreCase("ok") && systemStatus.equalsIgnoreCase("ok")) { break; } else { log.info(V, "..."); sleep(10); } } while (true); log.info(I, "Status checks successful."); //////////////////////////////////////////////////////////////////////// ///// run slave instances and supply userdata ////////////////////////// if (config.getSlaveInstanceCount() > 0) { String base64SlaveUserData = UserDataCreator.forSlave(masterInstance.getPrivateIpAddress(), masterInstance.getPrivateDnsName(), slaveDeviceMapper, this.config, environment.getKeypair()); log.info(V, "Slave Userdata:\n{}", new String(Base64.decodeBase64(base64SlaveUserData))); if (config.isUseSpotInstances()) { RequestSpotInstancesRequest slaveReq = new RequestSpotInstancesRequest(); slaveReq.withType(SpotInstanceType.OneTime).withInstanceCount(config.getSlaveInstanceCount()) .withLaunchGroup("lg_" + clusterId).withSpotPrice(Double.toString(config.getBidPrice())); LaunchSpecification slaveLaunchSpecification = new LaunchSpecification(); slaveLaunchSpecification .withInstanceType(InstanceType.fromValue(this.config.getSlaveInstanceType().getValue())) .withPlacement(spotInstancePlacement).withKeyName(this.config.getKeypair()) .withImageId(this.config.getSlaveImage()).withUserData(base64SlaveUserData) .withBlockDeviceMappings(slaveBlockDeviceMappings) .withNetworkInterfaces(slaveNetworkInterfaces); slaveReq.setLaunchSpecification(slaveLaunchSpecification); RequestSpotInstancesResult slaveReqResult = ec2.requestSpotInstances(slaveReq); List<SpotInstanceRequest> slaveReqResponses = slaveReqResult.getSpotInstanceRequests(); // collect all spotInstanceRequestIds ... List<String> spotInstanceRequestIds = new ArrayList<>(); for (SpotInstanceRequest requestResponse : slaveReqResponses) { spotInstanceRequestIds.add(requestResponse.getSpotInstanceRequestId()); } // wait for a second try { Thread.sleep(1000); } catch (InterruptedException e) { } log.info(V, "tag spot request instances"); // tag spot requests (slave) CreateTagsRequest ctr = new CreateTagsRequest(); ctr.withResources(spotInstanceRequestIds); ctr.withTags(bibigridid, username, new Tag().withKey("Name").withValue(PREFIX + "slave-" + clusterId)); /* Setting tags for spot requests can cause an amazon service exception, if the spot request returns an id, but the id isn't registered in spot request registy yet. */ int counter = 0; boolean finished = false; while (!finished) { try { ec2.createTags(ctr); finished = true; } catch (AmazonServiceException ase) { if (counter < 5) { log.warn("{} ... try again in 10 seconds.", ase.getMessage()); try { Thread.sleep(10000); } catch (InterruptedException e) { } counter++; } else { throw ase; } } } log.info("Waiting for slave instance(s) (spot request) to finish booting ..."); // wait for spot request (slave) finished slaveInstances = waitForInstances(waitForSpotInstances(spotInstanceRequestIds)); } else { RunInstancesRequest slaveReq = new RunInstancesRequest(); slaveReq.withInstanceType(InstanceType.fromValue(this.config.getSlaveInstanceType().getValue())) .withMinCount(this.config.getSlaveInstanceCount()) .withMaxCount(this.config.getSlaveInstanceCount()).withPlacement(instancePlacement) .withKeyName(this.config.getKeypair()).withImageId(this.config.getSlaveImage()) .withUserData(base64SlaveUserData).withBlockDeviceMappings(slaveBlockDeviceMappings) .withNetworkInterfaces(slaveNetworkInterfaces); RunInstancesResult slaveReqResult = ec2.runInstances(slaveReq); String slaveReservationId = slaveReqResult.getReservation().getReservationId(); // create a list of all slave instances List<String> slaveInstanceListIds = new ArrayList<>(); for (Instance i : slaveReqResult.getReservation().getInstances()) { slaveInstanceListIds.add(i.getInstanceId()); } log.info("Waiting for slave instance(s) to finish booting ..."); slaveInstances = waitForInstances(slaveInstanceListIds); } ///////////////////////////////////////////// //// Waiting for master instance to run //// log.info(I, "Slave instance(s) is now running!"); //////////////////////////////////// //// Tagging all slaves with a name for (Instance si : slaveInstances) { CreateTagsRequest slaveNameTagRequest = new CreateTagsRequest(); slaveNameTagRequest.withResources(si.getInstanceId()).withTags(bibigridid, username, new Tag().withKey("Name").withValue(PREFIX + "slave-" + clusterId)); ec2.createTags(slaveNameTagRequest); } } else { log.info("No Slave instance(s) requested !"); } ////////////////////////////////// ////// post configure master configureMaster(); //////////////////////////////////// //// Human friendly output StringBuilder sb = new StringBuilder(); sb.append("\n You might want to set the following environment variable:\n\n"); sb.append("export BIBIGRID_MASTER=").append(masterInstance.getPublicIpAddress()).append("\n\n"); sb.append("You can then log on the master node with:\n\n").append("ssh -i ") .append(config.getIdentityFile()).append(" ubuntu@$BIBIGRID_MASTER\n\n"); sb.append("The cluster id of your started cluster is : ").append(clusterId).append("\n\n"); sb.append("The can easily terminate the cluster at any time with :\n").append("./bibigrid -t ") .append(clusterId).append(" "); if (getConfig().isAlternativeConfigFile()) { sb.append("-o ").append(config.getAlternativeConfigPath()).append(" "); } sb.append("\n"); log.info(sb.toString()); //////////////////////////////////// //// Grid Properties file if (getConfig().getGridPropertiesFile() != null) { Properties gp = new Properties(); gp.setProperty("BIBIGRID_MASTER", masterInstance.getPublicIpAddress()); gp.setProperty("IdentityFile", getConfig().getIdentityFile().toString()); gp.setProperty("clusterId", clusterId); if (getConfig().isAlternativeConfigFile()) { gp.setProperty("AlternativeConfigFile", config.getAlternativeConfigPath()); } try { gp.store(new FileOutputStream(getConfig().getGridPropertiesFile()), "Autogenerated by BiBiGrid"); } catch (IOException e) { log.error(I, "Exception while creating grid properties file : " + e.getMessage()); } } return true; }