List of usage examples for com.amazonaws AmazonServiceException getStatusCode
public int getStatusCode()
From source file:com.ub.ml.S3Sample.java
License:Open Source License
public static void main(String[] args) throws IOException { /*//from w ww . j av a 2 s. c o m * 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 */ AmazonS3 s3 = new AmazonS3Client(); Region usWest2 = Region.getRegion(Regions.US_EAST_1); 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.uiintl.backup.agent.AwsBackupAgent.java
License:Open Source License
void handleAwsException(AmazonClientException ace) { if (ace instanceof AmazonServiceException) { AmazonServiceException ase = (AmazonServiceException) ace; logger.error(// w w w . ja va2 s. c o m "Caught an AmazonServiceException, which means your request made it to Amazon S3, but was rejected with an error response for some reason."); logger.error("Error Message: {}", ase.getMessage()); logger.error("HTTP Status Code: {}", ase.getStatusCode()); logger.error("AWS Error Code: {}", ase.getErrorCode()); logger.error("Error Type: {}", ase.getErrorType()); logger.error("Request ID: {}", ase.getRequestId()); logger.error("Stacktrace: ", ase); } else { logger.error( "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."); logger.error("Error Message: {}", ace.getMessage()); } }
From source file:com.uiintl.backup.agent.samples.S3Sample.java
License:Open Source License
public static void main2(String[] args) throws IOException { /*/*from ww w . j a va 2s . c o m*/ * This credentials provider implementation loads your AWS credentials * from a properties file at the root of your classpath. * * Important: Be sure to fill in your AWS access credentials in the * AwsCredentials.properties file before you try to run this * sample. * http://aws.amazon.com/security-credentials */ AmazonS3 s3 = new AmazonS3Client(new ClasspathPropertiesFileCredentialsProvider()); Region usWest2 = Region.getRegion(Regions.AP_SOUTHEAST_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.vb.services.database.rds.MySQLDBUtils.java
private void updateMySQLDBInstanceSpecificationsConfig() { MySQLDBInstanceSpecificationsConfig mysqlDBInstanceSpecificationsConfig = createDefaultDBInstanceSpecificationsConfig(); this.mysqlDB.setDbInstanceSpecificationsConfig(mysqlDBInstanceSpecificationsConfig); try {/* ww w . j av a 2s . co m*/ this.createDBInstanceRequest.setEngine(this.mysqlDB.getDbInstanceSpecificationsConfig().getDbEngine()); this.createDBInstanceRequest .setLicenseModel(this.mysqlDB.getDbInstanceSpecificationsConfig().getLicenceModel()); this.createDBInstanceRequest .setEngineVersion(this.mysqlDB.getDbInstanceSpecificationsConfig().getDbVersion()); this.createDBInstanceRequest .setDBInstanceClass(this.mysqlDB.getDbInstanceSpecificationsConfig().getDbInstanceClass()); this.createDBInstanceRequest .setMultiAZ(this.mysqlDB.getDbInstanceSpecificationsConfig().getMultiAZDeployment()); this.createDBInstanceRequest .setStorageType(this.mysqlDB.getDbInstanceSpecificationsConfig().getStorageType()); this.createDBInstanceRequest .setAllocatedStorage(this.mysqlDB.getDbInstanceSpecificationsConfig().getAllocatedStorage()); } catch (AmazonServiceException ase) { System.out.println("ERROR : error during MySQLDBInstanceSpecificationsConfig setup."); System.out.println("Caught an AmazonServiceException"); 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()); ase.printStackTrace(); } catch (AmazonClientException ace) { System.out.println("ERROR : error during MySQLDBInstanceSpecificationsConfig setup."); System.out.println("Caught an AmazonClientException"); System.out.println("Error Message: " + ace.getMessage()); ace.printStackTrace(); } }
From source file:com.vb.services.database.rds.MySQLDBUtils.java
private void updateMySQLDBSettingsConfig(String dbInstanceIdentifier, String masterUserName, String masterPassword) {/*from ww w.ja v a 2s . c o m*/ MySQLDBSettingsConfig mysqlDBSettingsConfig = createDBSettingsConfig(dbInstanceIdentifier, masterUserName, masterPassword); this.mysqlDB.setDbSettingsConfig(mysqlDBSettingsConfig); try { this.createDBInstanceRequest .setDBInstanceIdentifier(this.mysqlDB.getDbSettingsConfig().getDbInstanceIdentifier()); this.createDBInstanceRequest.setMasterUsername(this.mysqlDB.getDbSettingsConfig().getMasterUserName()); this.createDBInstanceRequest .setMasterUserPassword(this.mysqlDB.getDbSettingsConfig().getMasterPassword()); } catch (AmazonServiceException ase) { System.out.println("ERROR : error during MySQLDBSettingsConfig setup."); System.out.println("Caught an AmazonServiceException"); 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()); ase.printStackTrace(); } catch (AmazonClientException ace) { System.out.println("ERROR : error during MySQLDBSettingsConfig setup."); System.out.println("Caught an AmazonClientException"); System.out.println("Error Message: " + ace.getMessage()); ace.printStackTrace(); } }
From source file:com.vb.services.database.rds.MySQLDBUtils.java
private void updateMySQLDBNetworkSecurityConfig() { MySQLDBNetworkSecurityConfig mysqlDBNetworkSecurityConfig = new MySQLDBNetworkSecurityConfig(); this.mysqlDB.setDbNetworkSecurityConfig(mysqlDBNetworkSecurityConfig); try {//from w w w .j a v a 2 s. com } catch (AmazonServiceException ase) { System.out.println("ERROR : error during MySQLDBNetworkSecurityConfig setup."); System.out.println("Caught an AmazonServiceException"); 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()); ase.printStackTrace(); } catch (AmazonClientException ace) { System.out.println("ERROR : error during MySQLDBNetworkSecurityConfig setup."); System.out.println("Caught an AmazonClientException"); System.out.println("Error Message: " + ace.getMessage()); ace.printStackTrace(); } }
From source file:com.venu.springmvc.dao.AmazonDynamoDBDAO.java
License:Open Source License
public static void main(String[] args) throws Exception { init();//from w w w . ja v a2 s .c om try { String tableName = "my-favorite-movies-table"; // 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)); // Create table if it does not exist yet TableUtils.createTableIfNotExists(dynamoDB, createTableRequest); // wait for the table to move into ACTIVE state TableUtils.waitUntilActive(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("Gundamma katha", 1989, "****", "James", "Sara", "Venu"); PutItemRequest putItemRequest = new PutItemRequest(tableName, item); PutItemResult putItemResult = dynamoDB.putItem(putItemRequest); System.out.println("Result: " + putItemResult); // Add another item item = newItem("Manadesam", 1980, "*****", "James", "Billy Bob", "Abburi"); putItemRequest = new PutItemRequest(tableName, item); putItemResult = dynamoDB.putItem(putItemRequest); System.out.println("Result: " + putItemResult); // 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:com.vmware.photon.controller.model.adapters.awsadapter.AWSEndpointAdapterService.java
License:Open Source License
private BiConsumer<AuthCredentialsServiceState, BiConsumer<ServiceErrorResponse, Throwable>> validate( EndpointConfigRequest body) {//from w w w . java 2 s. com return (credentials, callback) -> { String regionId = body.endpointProperties.get(REGION_KEY); AmazonEC2AsyncClient client = AWSUtils.getAsyncClient(credentials, regionId, this.clientManager.getExecutor(getHost())); // make a call to validate credentials client.describeAvailabilityZonesAsync(new DescribeAvailabilityZonesRequest(), new AsyncHandler<DescribeAvailabilityZonesRequest, DescribeAvailabilityZonesResult>() { @Override public void onError(Exception e) { if (e instanceof AmazonServiceException) { AmazonServiceException ase = (AmazonServiceException) e; if (ase.getStatusCode() == STATUS_CODE_UNAUTHORIZED) { ServiceErrorResponse r = Utils.toServiceErrorResponse(e); r.statusCode = STATUS_CODE_UNAUTHORIZED; callback.accept(r, e); } return; } callback.accept(null, e); } @Override public void onSuccess(DescribeAvailabilityZonesRequest request, DescribeAvailabilityZonesResult describeAvailabilityZonesResult) { callback.accept(null, null); } }); }; }
From source file:com.vmware.photon.controller.model.adapters.awsadapter.AWSInstanceService.java
License:Open Source License
private void validateAWSCredentials(final AWSAllocation aws) { if (aws.computeRequest.isMockRequest) { aws.awsOperation.complete();//from ww w . j a v a2 s . com return; } aws.amazonEC2Client = this.clientManager.getOrCreateEC2Client(aws.parentAuth, getRequestRegionId(aws), this, aws.computeRequest.taskReference, false); // make a call to validate credentials aws.amazonEC2Client.describeAvailabilityZonesAsync(new DescribeAvailabilityZonesRequest(), new AsyncHandler<DescribeAvailabilityZonesRequest, DescribeAvailabilityZonesResult>() { @Override public void onError(Exception e) { if (e instanceof AmazonServiceException) { AmazonServiceException ase = (AmazonServiceException) e; if (ase.getStatusCode() == STATUS_CODE_UNAUTHORIZED) { ServiceErrorResponse r = Utils.toServiceErrorResponse(e); r.statusCode = STATUS_CODE_UNAUTHORIZED; aws.awsOperation.fail(e, r); } return; } aws.awsOperation.fail(e); } @Override public void onSuccess(DescribeAvailabilityZonesRequest request, DescribeAvailabilityZonesResult describeAvailabilityZonesResult) { aws.awsOperation.complete(); } }); }
From source file:com.zhang.aws.dynamodb.AmazonDynamoDBSample.java
License:Open Source License
public static void main(String[] args) throws Exception { init();//w w w.ja v a 2 s . c o m try { String tableName = "my-favorite-movies-table"; // 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("Bill & Ted's Excellent Adventure", 1989, "****", "James", "Sara"); PutItemRequest putItemRequest = new PutItemRequest(tableName, item); PutItemResult putItemResult = dynamoDB.putItem(putItemRequest); System.out.println("Result: " + putItemResult); // Add another item item = newItem("Airplane", 1980, "*****", "James", "Billy Bob"); putItemRequest = new PutItemRequest(tableName, item); putItemResult = dynamoDB.putItem(putItemRequest); System.out.println("Result: " + putItemResult); // 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()); } }