List of usage examples for com.amazonaws AmazonServiceException getErrorCode
public String getErrorCode()
From source file:com.sludev.commons.vfs2.provider.s3.SS3FileObject.java
License:Apache License
private boolean objectExists(String cont, String path) { boolean res = false; try {// www . j a v a 2s.co m S3Object object = fileSystem.getClient().getObject(cont, path); res = true; } catch (AmazonServiceException ex) { String errorCode = ex.getErrorCode(); if (!errorCode.equals("NoSuchKey")) { throw ex; } } return res; }
From source file:com.snapdeal.scm.core.service.impl.S3StorageServiceImpl.java
License:Open Source License
@Override public S3Object getObjectFromS3(String key) { try {//from www .j a v a 2 s. c o m AmazonS3URI uri = new AmazonS3URI(key); GetObjectRequest rangeObjectRequest = new GetObjectRequest(uri.getBucket(), uri.getKey()); S3Object s3object = s3Client.getObject(rangeObjectRequest); return s3object; } catch (AmazonServiceException ase) { LOG.info("Caught an AmazonServiceException, which" + " means your request made it " + "to Amazon S3, but was rejected with an error response" + " for some reason."); LOG.info("Error Message: " + ase.getMessage()); LOG.info("HTTP Status Code: " + ase.getStatusCode()); LOG.info("AWS Error Code: " + ase.getErrorCode()); LOG.info("Error Type: " + ase.getErrorType()); LOG.info("Request ID: " + ase.getRequestId()); throw ase; } catch (AmazonClientException ace) { LOG.info("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."); LOG.info("Error Message: " + ace.getMessage()); throw ace; } }
From source file:com.springboot.demo.framework.aws.s3.S3Sample.java
License:Open Source License
public static void main(String[] args) throws IOException { /*// w w w .ja 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 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);// w w w .j a v a 2s .c om 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 { /*//from www .j a v a2s . com * 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 w ww .j ava 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[] = { "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 va2 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();// w w w. j av a 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 = 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 { /*// ww w . ja v a2s.com * 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.telenav.amazon.s3.AmazonClientManager.java
License:Open Source License
public boolean wipeCredentialsOnAuthError(AmazonServiceException ex) { // For S3/* ww w . j a va2 s.c om*/ // http://docs.amazonwebservices.com/AmazonS3/latest/API/ErrorResponses.html#ErrorCodeList if (ex.getErrorCode().equals("AccessDenied") || ex.getErrorCode().equals("BadDigest") || ex.getErrorCode().equals("CredentialsNotSupported") || ex.getErrorCode().equals("ExpiredToken") || ex.getErrorCode().equals("InternalError") || ex.getErrorCode().equals("InvalidAccessKeyId") || ex.getErrorCode().equals("InvalidPolicyDocument") || ex.getErrorCode().equals("InvalidToken") || ex.getErrorCode().equals("NotSignedUp") || ex.getErrorCode().equals("RequestTimeTooSkewed") || ex.getErrorCode().equals("SignatureDoesNotMatch") || ex.getErrorCode().equals("TokenRefreshRequired")) { clearCredentials(); return true; } return false; }