List of usage examples for com.amazonaws AmazonServiceException getStatusCode
public int getStatusCode()
From source file:com.springboot.demo.framework.aws.s3.S3Sample.java
License:Open Source License
public static void main(String[] args) throws IOException { /*//w ww.j ava 2s. co m * The ProfileCredentialsProvider will return your [default] * credential profile by reading from the credentials file located at * (~/.aws/credentials). */ AWSCredentials basicCredentials = new BasicAWSCredentials(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY); AWSCredentials credentials = null; try { credentials = new ProfileCredentialsProvider().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 (~/.aws/credentials), and is in valid format.", e); } /* * Create S3 Client */ AmazonS3 s3 = AmazonS3ClientBuilder.defaultClient(); Region usWest2 = Region.getRegion(Regions.US_WEST_2); s3.setRegion(usWest2); String bucketName = "my-first-s3-bucket-" + UUID.randomUUID(); String key = "MyObjectKey"; System.out.println("==========================================="); System.out.println("Getting Started with Amazon S3"); System.out.println("===========================================\n"); try { /* * Create a new S3 bucket - Amazon S3 bucket names are globally unique, * so once a bucket name has been taken by any user, you can't create * another bucket with that same name. * * You can optionally specify a location for your bucket if you want to * keep your data closer to your applications or users. */ System.out.println("Creating bucket " + bucketName + "\n"); s3.createBucket(bucketName); /* * List the buckets in your account */ System.out.println("Listing buckets"); for (Bucket bucket : s3.listBuckets()) { System.out.println(" - " + bucket.getName()); } System.out.println(); /* * Upload an object to your bucket - You can easily upload a file to * S3, or upload directly an InputStream if you know the length of * the data in the stream. You can also specify your own metadata * when uploading to S3, which allows you set a variety of options * like content-type and content-encoding, plus additional metadata * specific to your applications. */ System.out.println("Uploading a new object to S3 from a file\n"); s3.putObject(new PutObjectRequest(bucketName, key, createSampleFile())); /* * Returns an URL for the object stored in the specified bucket and key */ URL url = s3.getUrl(bucketName, key); System.out.println("upload file url : " + url.toString()); /* * Download an object - When you download an object, you get all of * the object's metadata and a stream from which to read the contents. * It's important to read the contents of the stream as quickly as * possibly since the data is streamed directly from Amazon S3 and your * network connection will remain open until you read all the data or * close the input stream. * * GetObjectRequest also supports several other options, including * conditional downloading of objects based on modification times, * ETags, and selectively downloading a range of an object. */ System.out.println("Downloading an object"); S3Object object = s3.getObject(new GetObjectRequest(bucketName, key)); System.out.println("Content-Type: " + object.getObjectMetadata().getContentType()); displayTextInputStream(object.getObjectContent()); /* * List objects in your bucket by prefix - There are many options for * listing the objects in your bucket. Keep in mind that buckets with * many objects might truncate their results when listing their objects, * so be sure to check if the returned object listing is truncated, and * use the AmazonS3.listNextBatchOfObjects(...) operation to retrieve * additional results. */ System.out.println("Listing objects"); ObjectListing objectListing = s3 .listObjects(new ListObjectsRequest().withBucketName(bucketName).withPrefix("My")); for (S3ObjectSummary objectSummary : objectListing.getObjectSummaries()) { System.out.println( " - " + objectSummary.getKey() + " " + "(size = " + objectSummary.getSize() + ")"); } System.out.println(); /* * Delete an object - Unless versioning has been turned on for your bucket, * there is no way to undelete an object, so use caution when deleting objects. */ System.out.println("Deleting an object\n"); s3.deleteObject(bucketName, key); /* * Delete a bucket - A bucket must be completely empty before it can be * deleted, so remember to delete any objects from your buckets before * you try to delete them. */ System.out.println("Deleting bucket " + bucketName + "\n"); s3.deleteBucket(bucketName); } 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 " + "a serious internal problem 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:com.springboot.jms.AWSJmsSend.java
License:Open Source License
public static void testAWSQueue() throws Exception { AmazonSQS sqs = new AmazonSQSClient(new BasicAWSCredentials("", "")); Region usWest2 = Region.getRegion(Regions.US_EAST_1); sqs.setRegion(usWest2);// ww w . j a va2 s .c o m System.out.println("==========================================="); System.out.println("Getting Started with Amazon SQS"); System.out.println("===========================================\n"); try { String myQueueUrl = "https://sqs.us-east-1.amazonaws.com/918558451804/PetQueue"; // Send a message System.out.println("Sending a message to MyQueue.\n"); for (int i = 1; i <= 11; i++) { sqs.sendMessage(new SendMessageRequest(myQueueUrl, "This is my message number " + i)); Thread.sleep(1000); } } catch (AmazonServiceException ase) { System.out.println("Caught an AmazonServiceException, which means your request made it " + "to Amazon SQS, 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 SQS, such as not " + "being able to access the network."); System.out.println("Error Message: " + ace.getMessage()); } }
From source file:com.springboot.jms.AWSJmsSendAndReceive.java
License:Open Source License
public static void testAWSQueue() throws Exception { /*// 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 * (~/.aws/credentials). */ /* AWSCredentials credentials = null; try { credentials = new ProfileCredentialsProvider().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 (~/.aws/credentials), and is in valid format.", e); } AmazonSQS sqs = new AmazonSQSClient(credentials);*/ AmazonSQS sqs = new AmazonSQSClient(new BasicAWSCredentials("", "")); Region usWest2 = Region.getRegion(Regions.US_EAST_1); sqs.setRegion(usWest2); System.out.println("==========================================="); System.out.println("Getting Started with Amazon SQS"); System.out.println("===========================================\n"); try { /* // Create a queue System.out.println("Creating a new SQS queue called MyQueue.\n"); CreateQueueRequest createQueueRequest = new CreateQueueRequest("PetQueue"); String myQueueUrl = sqs.createQueue(createQueueRequest).getQueueUrl();*/ String myQueueUrl = "https://sqs.us-east-1.amazonaws.com/918558451804/PetQueue"; // List queues System.out.println("Listing all queues in your account.\n"); for (String queueUrl : sqs.listQueues().getQueueUrls()) { System.out.println(" QueueUrl: " + queueUrl); } System.out.println(); // Send a message System.out.println("Sending a message to MyQueue.\n"); sqs.sendMessage(new SendMessageRequest(myQueueUrl, "This is my message text.")); // Receive messages System.out.println("Receiving messages from MyQueue.\n"); ReceiveMessageRequest receiveMessageRequest = new ReceiveMessageRequest(myQueueUrl); List<Message> messages = sqs.receiveMessage(receiveMessageRequest).getMessages(); for (Message message : messages) { System.out.println(" Message"); System.out.println(" MessageId: " + message.getMessageId()); System.out.println(" ReceiptHandle: " + message.getReceiptHandle()); System.out.println(" MD5OfBody: " + message.getMD5OfBody()); System.out.println(" Body: " + message.getBody()); for (Entry<String, String> entry : message.getAttributes().entrySet()) { System.out.println(" Attribute"); System.out.println(" Name: " + entry.getKey()); System.out.println(" Value: " + entry.getValue()); } } System.out.println(); // Delete a message System.out.println("Deleting a message.\n"); String messageReceiptHandle = messages.get(0).getReceiptHandle(); sqs.deleteMessage(new DeleteMessageRequest(myQueueUrl, messageReceiptHandle)); /* // Delete a queue System.out.println("Deleting the test queue.\n"); sqs.deleteQueue(new DeleteQueueRequest(myQueueUrl));*/ } catch (AmazonServiceException ase) { System.out.println("Caught an AmazonServiceException, which means your request made it " + "to Amazon SQS, 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 SQS, such as not " + "being able to access the network."); System.out.println("Error Message: " + ace.getMessage()); } }
From source file:com.stockcloud.deletestock.java
License:Open Source License
public static void main(String[] args) throws Exception { init();/*from ww w . j a va 2 s .co m*/ String csvString; URL url = null; URLConnection urlConn = null; InputStreamReader inStream = null; BufferedReader buff = null; // static AmazonDynamoDBClient dynamoDB; // String jb=symbol.substring(0, 3)+"+"+symbol.substring(3, 7); // System.out.println(jb); // String[] newsymbol=symbol.split(","); int i = 0; String symbol[] = { "ABT", "ABBV", "ACE", "ACN", "ACT", "ADBE", "ADT", "AET", "AFL", "ARG", "AA", "ALL", "ALTR", "AMZN", "AAPL", "BLL", "BAC", "BMS", "BBY", "BLK", "BSX", "CA", "COF", "CCL", "CAT", "CERN", "CVX", "CSCO", "COH", "KO", "COST", "DVN", "DTV", "DG", "DOV", "DOW", "DUK", "EBAY", "EOG", "EMR", "EFX", "EQR", "EXPE", "ESRX", "FB", "FE", "FLIR", "FMC", "F", "FOSL", "GME", "GPS", "GD", "GE", "GIS", "GS", "GOOG", "HOG", "HRS", "HCP", "HAS", "HES", "HD", "HST", "HUM", "ITW", "INTC", "ICE", "IBM", "INTU", "IVZ", "JOY", "JNPR", "KEY", "KMI", "KLAC", "KSS", "KR", "LB", "LRCX", "LM", "LEN", "LNC", "LOW", "MAC", "MMM", "M", "MAR", "MA", "MCD", "MRK", "MET", "MU", "MSFT", "MNST", "MS", "MOS", "MUR", "MYL" }; // String // Num[]={"1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","3","2","3","4","5","6","7","8","9","10","1","2","3","4","5","6","7","8","9","10","1","2","3","4","5","6","7","8","9","10","1","2","3","4","5","6","7","8","9","10","1","2","3","4","5","6","7","8","9","10","1","2","3","4","5","6","7","8","9","10",} // loop to get the current data of every stocks(100) while (i < symbol.length) { url = new URL("http://quote.yahoo.com/d/quotes.csv?s=" + symbol[i] + "&f=osl1d1t1c1ohgv&e=.csv"); urlConn = url.openConnection(); inStream = new InputStreamReader(urlConn.getInputStream()); buff = new BufferedReader(inStream); // get the quote as a csv string csvString = buff.readLine(); // while(csvString!=null) // { // parse the csv string StringTokenizer tokenizer = new StringTokenizer(csvString, ","); String open = tokenizer.nextToken(); String ticker = tokenizer.nextToken(); String price = tokenizer.nextToken(); String tradeDate = tokenizer.nextToken(); String tradeTime = tokenizer.nextToken(); System.out.println( "Symbol: " + ticker + " Price: " + price + " Date: " + tradeDate + " Time: " + tradeTime); // table try { String tableName = symbol[i] + "current"; // 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("Time").withKeyType(KeyType.HASH)) .withAttributeDefinitions(new AttributeDefinition().withAttributeName("Time") .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); } Map<String, AttributeValue> item = newItem(ticker, price, tradeDate, tradeTime, open); PutItemRequest putItemRequest = new PutItemRequest(tableName, item); PutItemResult putItemResult = dynamoDB.putItem(putItemRequest); System.out.println("Result: " + putItemResult); } 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()); } // try over i++; } // whileover inStream.close(); buff.close(); }
From source file:com.stockcloud.searchstock.java
License:Open Source License
public List<String> search(String args) throws Exception { init();// w w w .j a v a 2 s .c om try { String tableName = args + "current"; // check table if it does not exist yet if (Tables.doesTableExist(dynamoDB, tableName)) { System.out.println("Table " + tableName + " is already ACTIVE"); } // Scan items ScanRequest scanRequest = new ScanRequest(tableName); ScanResult scanResult = dynamoDB.scan(scanRequest); String PK = null; int max = 0; String maxapm = "am"; String time; String apm; // scan all the item in the table and then delete them by PK for (Map<String, AttributeValue> item : scanResult.getItems()) { PK = getPK(item); // System.out.println(PK); time = PK.substring(1, 2) + PK.substring(3, 5); apm = PK.substring(5, 7); System.out.println(time); System.out.println(apm); int i = Integer.parseInt(time); System.out.println(i); if (maxapm.equals("am")) { if (apm.equals("am")) { if (i > max) { maxapm = apm; max = i; } } else { maxapm = apm; max = i; } } else { if (apm.equals("pm")) { if (i > max) { maxapm = apm; max = i; } } } } // loop end // find the max time System.out.println(max); System.out.println(maxapm); String s = String.valueOf(max); String aim = '"' + s.substring(0, 1) + ":" + s.substring(1) + maxapm + '"'; System.out.println(aim); // search for the max time String currenttime = null; String currentprice = null; String currentopen = null; HashMap<String, AttributeValue> key = new HashMap<String, AttributeValue>(); key.put("Time", new AttributeValue().withS(aim)); GetItemRequest getItemRequest = new GetItemRequest().withTableName(tableName).withKey(key); GetItemResult result = dynamoDB.getItem(getItemRequest); Map<String, AttributeValue> map = result.getItem(); for (Map.Entry<String, AttributeValue> item : map.entrySet()) { String attributeName = item.getKey(); AttributeValue value = item.getValue(); if (attributeName.equals("Time")) currenttime = value.getS(); if (attributeName.equals("price")) currentprice = value.getS(); if (attributeName.equals("open")) currentopen = value.getS(); } System.out.println(currenttime); System.out.println(currentprice); System.out.println(currentopen); List<String> currentStockinfoList = new ArrayList<String>(); currentStockinfoList.add(currenttime); currentStockinfoList.add(currentopen); currentStockinfoList.add(currentprice); return currentStockinfoList; } 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()); } List<String> errorList = new ArrayList<String>(); errorList.add("wrong!!"); return errorList; }
From source file:com.stockcloud.updatestock.java
License:Open Source License
public void update(String stockName) throws Exception { init();//from w w w .j a va 2 s .c o m String csvString; URL url = null; URLConnection urlConn = null; InputStreamReader inStream = null; BufferedReader buff = null; // static AmazonDynamoDBClient dynamoDB; // String jb=symbol.substring(0, 3)+"+"+symbol.substring(3, 7); // System.out.println(jb); // String[] newsymbol=symbol.split(","); int i = 0; String symbol = stockName; // String symbol[] = { "ABT", "ABBV", "ACE", "ACN", "ACT", "ADBE", // "ADT", // "AET", "AFL", "ARG", "AA", "ALL", "ALTR", "AMZN", "AAPL", // "BLL", "BAC", "BMS", "BBY", "BLK", "BSX", "CA", "COF", "CCL", // "CAT", "CERN", "CVX", "CSCO", "COH", "KO", "COST", "DVN", // "DTV", "DG", "DOV", "DOW", "DUK", "EBAY", "EOG", "EMR", "EFX", // "EQR", "EXPE", "ESRX", "FB", "FE", "FLIR", "FMC", "F", "FOSL", // "GME", "GPS", "GD", "GE", "GIS", "GS", "GOOG", "HOG", "HRS", // "HCP", "HAS", "HES", "HD", "HST", "HUM", "ITW", "INTC", "ICE", // "IBM", "INTU", "IVZ", "JOY", "JNPR", "KEY", "KMI", "KLAC", // "KSS", "KR", "LB", "LRCX", "LM", "LEN", "LNC", "LOW", "MAC", // "MMM", "M", "MAR", "MA", "MCD", "MRK", "MET", "MU", "MSFT", // "MNST", "MS", "MOS", "MUR", "MYL" }; // String // Num[]={"1","2","3","4","5","6","7","8","9","10","11","12","13","14", // "15","16","17","18","19","20","21","22","23","24","25","26","27","28", // "29","30","3","2","3","4","5","6","7","8","9","10","1","2","3","4","5", // "6","7","8","9","10","1","2","3","4","5","6","7","8","9","10","1","2", // "3","4","5","6","7","8","9","10","1","2","3","4","5","6","7","8","9", // "10","1","2","3","4","5","6","7","8","9","10",} // loop to get the current data of every stocks(100) url = new URL("http://quote.yahoo.com/d/quotes.csv?s=" + symbol + "&f=osl1d1t1c1ohgv&e=.csv"); urlConn = url.openConnection(); inStream = new InputStreamReader(urlConn.getInputStream()); buff = new BufferedReader(inStream); // get the quote as a csv string csvString = buff.readLine(); // while(csvString!=null) // { // parse the csv string StringTokenizer tokenizer = new StringTokenizer(csvString, ","); String open = tokenizer.nextToken(); String ticker = tokenizer.nextToken(); String price = tokenizer.nextToken(); String tradeDate = tokenizer.nextToken(); String tradeTime = tokenizer.nextToken(); System.out .println("Symbol: " + ticker + " Price: " + price + " Date: " + tradeDate + " Time: " + tradeTime); // table try { String tableName = symbol + "current"; // 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("Time").withKeyType(KeyType.HASH)) .withAttributeDefinitions(new AttributeDefinition().withAttributeName("Time") .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); } Map<String, AttributeValue> item = newItem(ticker, price, tradeDate, tradeTime, open); PutItemRequest putItemRequest = new PutItemRequest(tableName, item); PutItemResult putItemResult = dynamoDB.putItem(putItemRequest); System.out.println("Result: " + putItemResult); } 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()); } // try over inStream.close(); buff.close(); }
From source file:com.supprema.utils.S3Sample.java
License:Open Source License
public static void main(String[] args) throws IOException { /*/*from ww w. j a va 2 s. co m*/ * The ProfileCredentialsProvider will return your [fabiano-user-s3] * credential profile by reading from the credentials file located at * (/Users/fabianorodriguesmatias/.aws/credentials). */ AWSCredentials credentials = null; try { credentials = new ProfileCredentialsProvider("fabiano-user-s3").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 (/Users/fabianorodriguesmatias/.aws/credentials), and is in valid format.", e); } AmazonS3 s3 = new AmazonS3Client(credentials); Region usWest2 = Region.getRegion(Regions.US_WEST_2); s3.setRegion(usWest2); String bucketName = "my-first-s3-bucket-" + UUID.randomUUID(); String key = "MyObjectKey"; System.out.println("==========================================="); System.out.println("Getting Started with Amazon S3"); System.out.println("===========================================\n"); try { /* * Create a new S3 bucket - Amazon S3 bucket names are globally unique, * so once a bucket name has been taken by any user, you can't create * another bucket with that same name. * * You can optionally specify a location for your bucket if you want to * keep your data closer to your applications or users. */ System.out.println("Creating bucket " + bucketName + "\n"); s3.createBucket(bucketName); /* * List the buckets in your account */ System.out.println("Listing buckets"); for (Bucket bucket : s3.listBuckets()) { System.out.println(" - " + bucket.getName()); } System.out.println(); /* * Upload an object to your bucket - You can easily upload a file to * S3, or upload directly an InputStream if you know the length of * the data in the stream. You can also specify your own metadata * when uploading to S3, which allows you set a variety of options * like content-type and content-encoding, plus additional metadata * specific to your applications. */ System.out.println("Uploading a new object to S3 from a file\n"); s3.putObject(new PutObjectRequest(bucketName, key, createSampleFile())); /* * Download an object - When you download an object, you get all of * the object's metadata and a stream from which to read the contents. * It's important to read the contents of the stream as quickly as * possibly since the data is streamed directly from Amazon S3 and your * network connection will remain open until you read all the data or * close the input stream. * * GetObjectRequest also supports several other options, including * conditional downloading of objects based on modification times, * ETags, and selectively downloading a range of an object. */ System.out.println("Downloading an object"); S3Object object = s3.getObject(new GetObjectRequest(bucketName, key)); System.out.println("Content-Type: " + object.getObjectMetadata().getContentType()); displayTextInputStream(object.getObjectContent()); /* * List objects in your bucket by prefix - There are many options for * listing the objects in your bucket. Keep in mind that buckets with * many objects might truncate their results when listing their objects, * so be sure to check if the returned object listing is truncated, and * use the AmazonS3.listNextBatchOfObjects(...) operation to retrieve * additional results. */ System.out.println("Listing objects"); ObjectListing objectListing = s3 .listObjects(new ListObjectsRequest().withBucketName(bucketName).withPrefix("My")); for (S3ObjectSummary objectSummary : objectListing.getObjectSummaries()) { System.out.println( " - " + objectSummary.getKey() + " " + "(size = " + objectSummary.getSize() + ")"); } System.out.println(); /* * Delete an object - Unless versioning has been turned on for your bucket, * there is no way to undelete an object, so use caution when deleting objects. */ System.out.println("Deleting an object\n"); s3.deleteObject(bucketName, key); /* * Delete a bucket - A bucket must be completely empty before it can be * deleted, so remember to delete any objects from your buckets before * you try to delete them. */ System.out.println("Deleting bucket " + bucketName + "\n"); s3.deleteBucket(bucketName); } 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 " + "a serious internal problem 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:com.tango.BucketSyncer.KeyJobs.S32GCSKeyCopyJob.java
License:Apache License
boolean keyCopied(ObjectMetadata sourceMetadata) { boolean copied = false; String key = summary.getKey(); MirrorOptions options = context.getOptions(); boolean verbose = options.isVerbose(); int maxRetries = options.getMaxRetries(); MirrorStats stats = context.getStats(); InputStreamContent mediaContent = null; for (int tries = 0; tries < maxRetries; tries++) { if (verbose) { log.info("copying (try # {} ): {} to: {}", new Object[] { tries, key, keydest }); }/*from w w w.j a v a 2 s.c o m*/ //get object from S3 //deal with exception that the object has been deleted when trying to fetch it from S3 S3Object s3object = null; try { s3object = s3Client.getObject(new GetObjectRequest(options.getSourceBucket(), key)); } catch (AmazonServiceException e) { log.error("Failed to fetch object from S3. Object {} may have been deleted: {}", key, e); } catch (Exception e) { log.error("Failed to fetch object from S3. Object {} may have been deleted: {}", key, e); } if (s3object != null) { InputStream inputStream = s3object.getObjectContent(); String type = s3object.getObjectMetadata().getContentType(); mediaContent = new InputStreamContent(type, inputStream); String etag = s3object.getObjectMetadata().getETag(); StorageObject objectMetadata = new StorageObject().setMetadata(ImmutableMap.of("Etag", etag)); Storage.Objects.Insert insertObject = null; try { insertObject = gcsClient.objects().insert(options.getDestinationBucket(), objectMetadata, mediaContent); } catch (IOException e) { log.error("Failed to create insertObject of GCS ", e); } insertObject.setName(key); insertObject.getMediaHttpUploader().setProgressListener(new CustomUploadProgressListener()) .setDisableGZipContent(true); // For small files, you may wish to call setDirectUploadEnabled(true), to // reduce the number of HTTP requests made to the server. if (mediaContent.getLength() > 0 && mediaContent.getLength() <= 2 * 1000 * 1000 /* 2MB */) { insertObject.getMediaHttpUploader().setDirectUploadEnabled(true); } try { stats.copyCount.incrementAndGet(); insertObject.execute(); stats.bytesCopied.addAndGet(sourceMetadata.getContentLength()); if (verbose) log.info("Successfully copied (on try # {} ): {} to: {} in GCS", new Object[] { tries, key, keydest }); copied = true; break; } catch (GoogleJsonResponseException e) { if (e.getStatusCode() == HttpStatus.SC_NOT_FOUND) { log.error("Failed to access GCS bucket. Check bucket name: ", e); System.exit(1); } } catch (IOException e) { log.error("GCS exception copying (try # {} ) {} to: {} : {}", new Object[] { tries, key, keydest, e }); } } try { Thread.sleep(10); } catch (InterruptedException e) { log.error("interrupted while waiting to retry key: {}, {}", key, e); return copied; } } return copied; }
From source file:com.topera.epoch.service.S3Util.java
License:Open Source License
public static void main(String[] args) throws IOException { /*/* w w w . ja v a 2 s . com*/ * Create your credentials file at ~/.aws/credentials (C:\Users\USER_NAME\.aws\credentials for Windows users) * and save the following lines after replacing the underlined values with your own. * * [default] * aws_access_key_id = YOUR_ACCESS_KEY_ID * aws_secret_access_key = YOUR_SECRET_ACCESS_KEY */ AWSCredentials creds = new AWSCredentials() { public String getAWSSecretKey() { // TODO Auto-generated method stub return "5VVtmI7vcecuVbw8JsG4uo2O1/9RwwLHrTT01Itz"; } public String getAWSAccessKeyId() { // TODO Auto-generated method stub return "AKIAJCMYALI46A2DIPRQ"; } }; AmazonS3 s3 = new AmazonS3Client(creds); Region usWest2 = Region.getRegion(Regions.US_WEST_2); s3.setRegion(usWest2); String bucketName = "my-first-s3-bucket-" + UUID.randomUUID(); String key = "MyObjectKey"; System.out.println("==========================================="); System.out.println("Getting Started with Amazon S3"); System.out.println("===========================================\n"); try { /* * Create a new S3 bucket - Amazon S3 bucket names are globally unique, * so once a bucket name has been taken by any user, you can't create * another bucket with that same name. * * You can optionally specify a location for your bucket if you want to * keep your data closer to your applications or users. */ System.out.println("Creating bucket " + bucketName + "\n"); s3.createBucket(bucketName); /* * List the buckets in your account */ System.out.println("Listing buckets"); for (Bucket bucket : s3.listBuckets()) { System.out.println(" - " + bucket.getName()); } System.out.println(); /* * Upload an object to your bucket - You can easily upload a file to * S3, or upload directly an InputStream if you know the length of * the data in the stream. You can also specify your own metadata * when uploading to S3, which allows you set a variety of options * like content-type and content-encoding, plus additional metadata * specific to your applications. */ System.out.println("Uploading a new object to S3 from a file\n"); s3.putObject(new PutObjectRequest(bucketName, key, createSampleFile())); /* * Download an object - When you download an object, you get all of * the object's metadata and a stream from which to read the contents. * It's important to read the contents of the stream as quickly as * possibly since the data is streamed directly from Amazon S3 and your * network connection will remain open until you read all the data or * close the input stream. * * GetObjectRequest also supports several other options, including * conditional downloading of objects based on modification times, * ETags, and selectively downloading a range of an object. */ System.out.println("Downloading an object"); S3Object object = s3.getObject(new GetObjectRequest(bucketName, key)); System.out.println("Content-Type: " + object.getObjectMetadata().getContentType()); displayTextInputStream(object.getObjectContent()); /* * List objects in your bucket by prefix - There are many options for * listing the objects in your bucket. Keep in mind that buckets with * many objects might truncate their results when listing their objects, * so be sure to check if the returned object listing is truncated, and * use the AmazonS3.listNextBatchOfObjects(...) operation to retrieve * additional results. */ System.out.println("Listing objects"); ObjectListing objectListing = s3 .listObjects(new ListObjectsRequest().withBucketName(bucketName).withPrefix("My")); for (S3ObjectSummary objectSummary : objectListing.getObjectSummaries()) { System.out.println( " - " + objectSummary.getKey() + " " + "(size = " + objectSummary.getSize() + ")"); } System.out.println(); /* * Delete an object - Unless versioning has been turned on for your bucket, * there is no way to undelete an object, so use caution when deleting objects. */ System.out.println("Deleting an object\n"); s3.deleteObject(bucketName, key); /* * Delete a bucket - A bucket must be completely empty before it can be * deleted, so remember to delete any objects from your buckets before * you try to delete them. */ System.out.println("Deleting bucket " + bucketName + "\n"); s3.deleteBucket(bucketName); } 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 " + "a serious internal problem 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:com.tweettrends.pravar.FilterStreamExample.java
License:Apache License
public static void main(String[] args) { final SimpleQueueService simpleQueueService = new SimpleQueueService(); final List<Future<String>> results = new ArrayList<Future<String>>(); final HashMap<Future<String>, Message> map = new HashMap<Future<String>, Message>(100); SimpleNotificationService simpleNotificationService = new SimpleNotificationService(); simpleNotificationService.subscribeToTopic(); try {//w w w. ja va 2s.c o m //simpleQueueService.createQueue(); simpleQueueService.listQueues(); } catch (AmazonServiceException ase) { System.out.println("Caught an AmazonServiceException, which means your request made it " + "to Amazon SQS, 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 SQS, such as not " + "being able to access the network."); System.out.println("Error Message: " + ace.getMessage()); } new Thread(new Runnable() { @Override public void run() { try { FilterStreamExample.run("9sB7Y7zyxFTgEpk87ZwuZMFZR", "TPvVJJ09FhQeduDR10xJw8t5LJ4i75uu6GYQefVtHt7ebUTgZi", "840399362987560960-MTKPBj2U67boTVP4ug6LWiUdvksF0gO", "adanfdOhMgPmil1TsWpD1vKvfdY6ErRVX2xCqPS6NgaEF", simpleQueueService); } catch (InterruptedException e) { System.out.println(e); } } }).start(); ExecutorService pool = Executors.newCachedThreadPool(); Thread thread = new Thread(new Runnable() { @Override public void run() { System.out.println("Entered runnable"); while (true) { Iterator<Future<String>> iterator = results.iterator(); try { sharedSemaphore.acquire(); while (iterator.hasNext()) { Future<String> result = iterator.next(); String sentiment = result.get(); Message message = map.get(result); // send sentiment and message to SNS System.out.println("Notifying SNS"); //simpleNotificationService.publishToTopic(message.getBody()); iterator.remove(); } } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); } finally { sharedSemaphore.release(); } } } }); thread.start(); while (true) { List<Message> messages = simpleQueueService.receiveMessages(); if (messages != null) { System.out.println("Received Messages!"); for (Message msg : messages) { System.out.println("Message body : " + msg.getBody()); } simpleQueueService.deleteMessages(messages); for (Message message : messages) { Worker worker = new Worker(message); Future<String> result = pool.submit(worker); try { sharedSemaphore.acquire(); results.add(result); map.put(result, message); } catch (InterruptedException e) { e.printStackTrace(); } finally { sharedSemaphore.release(); } } } } }