Example usage for com.amazonaws AmazonServiceException getErrorCode

List of usage examples for com.amazonaws AmazonServiceException getErrorCode

Introduction

In this page you can find the example usage for com.amazonaws AmazonServiceException getErrorCode.

Prototype

public String getErrorCode() 

Source Link

Document

Returns the AWS error code represented by this exception.

Usage

From source file:TableCreator.java

License:Open Source License

public static void main(String[] args) throws Exception {
    init();/*from w  ww .  j  a v  a 2s .co m*/
    long readCapacity = Long.valueOf(args[0]);
    long writeCapacity = Long.valueOf(args[1]);
    try {
        String tableName = "messages";

        //          DeleteTableRequest deleteTableRequest = new DeleteTableRequest(tableName);
        //          dynamoDB.deleteTable(deleteTableRequest);
        //          
        //          waitForTableToBecomeAvailable(tableName);

        // Create a table with a primary hash key named 'name', which holds a string
        CreateTableRequest createTableRequest = new CreateTableRequest().withTableName(tableName)
                .withKeySchema(new KeySchemaElement().withAttributeName("messageId").withKeyType(KeyType.HASH))
                .withAttributeDefinitions(new AttributeDefinition().withAttributeName("messageId")
                        .withAttributeType(ScalarAttributeType.S))
                .withProvisionedThroughput(new ProvisionedThroughput().withReadCapacityUnits(readCapacity)
                        .withWriteCapacityUnits(writeCapacity));
        TableDescription createdTableDescription = dynamoDB.createTable(createTableRequest)
                .getTableDescription();
        System.out.println("Created Table: " + createdTableDescription);

        // Wait for it to become active
        waitForTableToBecomeAvailable(tableName);

        // Describe our new table
        DescribeTableRequest describeTableRequest = new DescribeTableRequest().withTableName(tableName);
        TableDescription tableDescription = dynamoDB.describeTable(describeTableRequest).getTable();
        System.out.println("Table Description: " + tableDescription);

    } catch (AmazonServiceException ase) {
        System.out.println("Caught an AmazonServiceException, which means your request made it "
                + "to AWS, but was rejected with an error response for some reason.");
        System.out.println("Error Message:    " + ase.getMessage());
        System.out.println("HTTP Status Code: " + ase.getStatusCode());
        System.out.println("AWS Error Code:   " + ase.getErrorCode());
        System.out.println("Error Type:       " + ase.getErrorType());
        System.out.println("Request ID:       " + ase.getRequestId());
    } catch (AmazonClientException ace) {
        System.out.println("Caught an AmazonClientException, which means the client encountered "
                + "a serious internal problem while trying to communicate with AWS, "
                + "such as not being able to access the network.");
        System.out.println("Error Message: " + ace.getMessage());
    }
}

From source file:TableCreator.java

License:Open Source License

private static void waitForTableToBecomeAvailable(String tableName) {
    System.out.println("Waiting for " + tableName + " to become ACTIVE...");

    long startTime = System.currentTimeMillis();
    long endTime = startTime + (10 * 60 * 1000);
    while (System.currentTimeMillis() < endTime) {
        try {/*from www . j a  v a 2  s .c  o m*/
            Thread.sleep(1000 * 20);
        } catch (Exception e) {
        }
        try {
            DescribeTableRequest request = new DescribeTableRequest().withTableName(tableName);
            TableDescription tableDescription = dynamoDB.describeTable(request).getTable();
            String tableStatus = tableDescription.getTableStatus();
            System.out.println("  - current state: " + tableStatus);
            if (tableStatus.equals(TableStatus.ACTIVE.toString()))
                return;
        } catch (AmazonServiceException ase) {
            if (ase.getErrorCode().equalsIgnoreCase("ResourceNotFoundException") == false)
                throw ase;
        }
    }

    throw new RuntimeException("Table " + tableName + " never went active");
}

From source file:TableCreator.java

License:Open Source License

private static void waitForTableToDelete(String tableName) {
    System.out.println("Waiting for " + tableName + " to become ACTIVE...");

    long startTime = System.currentTimeMillis();
    long endTime = startTime + (10 * 60 * 1000);
    while (System.currentTimeMillis() < endTime) {
        try {/*  w  w w.  j a va 2s .  c o m*/
            Thread.sleep(1000 * 20);
        } catch (Exception e) {
        }
        try {
            DescribeTableRequest request = new DescribeTableRequest().withTableName(tableName);
            TableDescription tableDescription = dynamoDB.describeTable(request).getTable();
            String tableStatus = tableDescription.getTableStatus();
            System.out.println("  - current state: " + tableStatus);
            if (tableStatus.equals(TableStatus.ACTIVE.toString()))
                return;
        } catch (AmazonServiceException ase) {
            if (ase.getErrorCode().equalsIgnoreCase("ResourceNotFoundException") == false)
                throw ase;
        }
    }

    throw new RuntimeException("Table " + tableName + " never went active");
}

From source file:SNSMobilePush.java

License:Open Source License

public static void main(String[] args) throws IOException {
    /*//from  w ww. ja  va2 s .co  m
     * TODO: 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
     */
    AmazonSNS sns = new AmazonSNSClient(
            new PropertiesCredentials(SNSMobilePush.class.getResourceAsStream("AwsCredentials.properties")));

    sns.setEndpoint("https://sns.us-east-1.amazonaws.com");
    System.out.println("===========================================\n");
    System.out.println("Getting Started with Amazon SNS");
    System.out.println("===========================================\n");
    try {
        SNSMobilePush sample = new SNSMobilePush(sns);
        /* TODO: Uncomment the services you wish to use. */
        sample.demoAndroidAppNotification();
        // sample.demoKindleAppNotification();
        // sample.demoAppleAppNotification();
        // sample.demoAppleSandboxAppNotification();
        // sample.demoBaiduAppNotification();
        // sample.demoWNSAppNotification();
        // sample.demoMPNSAppNotification();
    } catch (AmazonServiceException ase) {
        System.out.println("Caught an AmazonServiceException, which means your request made it "
                + "to Amazon SNS, 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 SNS, such as not "
                + "being able to access the network.");
        System.out.println("Error Message: " + ace.getMessage());
    }
}

From source file:sqsAlertReceive.java

License:Open Source License

public static void main(String[] args) throws Exception {

    /*/*  www. j a v  a2s  .c om*/
     * The ProfileCredentialsProvider will return your [awsReilly]
     * credential profile by reading from the credentials file located at
     * (/Users/johnreilly/.aws/credentials).
     */
    AWSCredentials credentials = null;
    try {
        credentials = new ProfileCredentialsProvider("awsReilly").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/johnreilly/.aws/credentials), and is in valid format.", e);
    }

    AmazonSQS sqs = new AmazonSQSClient(credentials);
    Region usEast1 = Region.getRegion(Regions.US_EAST_1);
    sqs.setRegion(usEast1);
    String testQueue = "reillyInbound001";

    System.out.println("");
    System.out.println("===========================================");
    System.out.println("Getting Started with sqsAlertReceive");
    System.out.println("===========================================\n");

    try {
        // Create a queue
        //            System.out.println("Creating a new SQS queue called MyQueue.\n");
        //            CreateQueueRequest createQueueRequest = new CreateQueueRequest("MyQueue");
        //            String myQueueUrl = sqs.createQueue(createQueueRequest).getQueueUrl();

        // List queues
        System.out.println("Listing all queues in your account.");
        for (String queueUrl : sqs.listQueues().getQueueUrls()) {
            System.out.println("QueueUrl: " + queueUrl);
        }
        System.out.println();

        // Receive messages
        System.out.println("Receiving messages from " + testQueue + ".");
        ReceiveMessageRequest receiveMessageRequest = new ReceiveMessageRequest(testQueue);
        List<Message> messages = sqs.receiveMessage(receiveMessageRequest).getMessages();
        System.out.println("Message count for " + testQueue + ": " + messages.size() + "\n");

        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();

            // call a function to transform message
            // then send message to database persist queue

            System.out.println("Sending messages to alertsPersist.\n");
            sqs.sendMessage(new SendMessageRequest("alertPersist", message.getBody()));

            // delete message after sending to persist queue
            System.out.println("Deleting message.\n");
            String messageRecieptHandle = message.getReceiptHandle();
            sqs.deleteMessage(new DeleteMessageRequest(testQueue, messageRecieptHandle));

        }

    } 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:PutTestFile.java

License:Apache License

public static void main(String[] args) throws IOException {
    /*// w w  w  . j  a v a 2s . c  o m
     * 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 PropertiesCredentials(

            PutTestFile.class.getResourceAsStream("AwsCredentials.properties")));
    String bucketName = "dtccTest";

    String key = "largerfile.txt";

    System.out.println("===========================================");
    System.out.println("Getting Started with Amazon S3");
    System.out.println("===========================================\n");

    try {

        /*
         * List the buckets in your account
         */
        System.out.println("Listing buckets");
        for (Bucket bucket : s3.listBuckets()) {
            System.out.println(" - " + bucket.getName());
        }
        System.out.println();

        System.out.println("Uploading a new object to S3 from a file\n");

        PutObjectRequest request = new PutObjectRequest(bucketName, key, createSampleFile());
        request.setCannedAcl(CannedAccessControlList.PublicRead);
        s3.putObject(request);

    } catch (AmazonServiceException ase) {
        System.out.println("Caught an AmazonServiceException, whichmeans your request made it "
                + "to Amazon S3, but was rejected with an errorresponse 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, whichmeans the client encountered "
                + "a serious internal problem while trying tocommunicate with S3, "
                + "such as not being able to access the network.");
        System.out.println("Error Message: " + ace.getMessage());
    }
}

From source file:ProductCategoryPriceIndex.java

License:Open Source License

public static void main(String[] args) throws Exception {
    init();/*from  ww  w  .  j av  a  2 s  .  c  o m*/

    try {
        String tableName = "product_cat_pi";

        // Create table if it does not exist yet
        if (!Tables.doesTableExist(dynamoDB, tableName)) {
            System.out.println("Table " + tableName + " is does not exist");
        }

        // 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("TV", 2, "TV Type one", "TV Type two");
        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());
    }
}

From source file:SimpleQueueServiceMultiThread.java

License:Open Source License

@Override
public Long call() throws Exception {

    Long transformTimeDiff = 0L;/*from w w  w  .j a  v  a2 s. com*/

    long startDate = System.currentTimeMillis();

    try {

        System.out.println("Sending a message to MyQueue.\n");
        sqs.sendMessage(new SendMessageRequest(myQueueUrl, "This is my message text."));

    } 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());
    }

    long endDate = System.currentTimeMillis();

    transformTimeDiff = Long.valueOf(endDate - startDate);

    return transformTimeDiff;
}

From source file:EBS_S3.java

License:Open Source License

public static void main(String[] args) throws Exception {

    AWSCredentials credentials = new PropertiesCredentials(
            EBS_S3.class.getResourceAsStream("AwsCredentials.properties"));

    /*********************************************
     *  #1 Create Amazon Client object/* ww  w  .  j av  a2  s.co  m*/
     **********************************************/
    ec2 = new AmazonEC2Client(credentials);

    // We assume that we've already created an instance. Use the id of the instance.
    String instanceId = "i-278afe40"; //put your own instance id to test this code.

    try {

        /*********************************************
         *  #2.1 Create a volume
         *********************************************/
        // release
        System.out.println(ec2.describeVolumes());
        List<Volume> v_list = ec2.describeVolumes().getVolumes();
        System.out.println("Volume size: " + v_list.size());
        DeleteVolumeRequest rq;
        for (Volume v : v_list) {
            if (v.getVolumeId().compareTo("vol-343dd879") != 0) {
                rq = new DeleteVolumeRequest(v.getVolumeId());
                ec2.deleteVolume(rq);
            }
        }
        System.out.println(ec2.describeVolumes());
        System.out.println("Volume size: " + v_list.size());

        //create a volume
        System.out.println("2.1 Create a volume");
        CreateVolumeRequest cvr = new CreateVolumeRequest();
        cvr.setAvailabilityZone("us-east-1b");
        cvr.setSize(10); //size = 10 gigabytes
        CreateVolumeResult volumeResult = ec2.createVolume(cvr);
        String createdVolumeId = volumeResult.getVolume().getVolumeId();
        System.out.println("Created Volume ID: " + createdVolumeId);

        /*********************************************
         *  #2.2 Attach the volume to the instance
         *********************************************/
        System.out.println("2.2 Attach the volume to the instance");
        Thread.sleep(30000);
        AttachVolumeRequest avr = new AttachVolumeRequest();
        avr.setVolumeId(createdVolumeId);
        avr.setInstanceId(instanceId);
        avr.setDevice("/dev/sdf");
        ec2.attachVolume(avr);

        /*********************************************
         *  #2.3 Detach the volume from the instance
         *********************************************/
        System.out.println("2.3 Detach the volume from the instance");
        DetachVolumeRequest dvr = new DetachVolumeRequest();
        dvr.setVolumeId(createdVolumeId);
        dvr.setInstanceId(instanceId);
        ec2.detachVolume(dvr);

        /************************************************
        *    #3 S3 bucket and object
        ***************************************************/
        System.out.println("3 S3 bucket and object");
        s3 = new AmazonS3Client(credentials);

        //create bucket
        String bucketName = "cloud-xiangyao-bucket";
        s3.createBucket(bucketName);

        //set key
        String key = "object-name.txt";

        //set value
        File file = File.createTempFile("temp", ".txt");
        file.deleteOnExit();
        Writer writer = new OutputStreamWriter(new FileOutputStream(file));
        writer.write("This is a sample sentence.\r\nYes!");
        writer.close();

        //put object - bucket, key, value(file)
        s3.putObject(new PutObjectRequest(bucketName, key, file));

        //get object
        S3Object object = s3.getObject(new GetObjectRequest(bucketName, key));
        BufferedReader reader = new BufferedReader(new InputStreamReader(object.getObjectContent()));
        String data = null;
        while ((data = reader.readLine()) != null) {
            System.out.println(data);
        }

        /*********************************************
         *  #4 shutdown client object
         *********************************************/
        System.out.println("4 shutdown client object");
        ec2.shutdown();
        s3.shutdown();

    } catch (AmazonServiceException ase) {
        System.out.println("Caught Exception: " + ase.getMessage());
        System.out.println("Reponse Status Code: " + ase.getStatusCode());
        System.out.println("Error Code: " + ase.getErrorCode());
        System.out.println("Request ID: " + ase.getRequestId());
    }

}

From source file:NYSEQuery.java

License:Open Source License

private static void query(String tableName) {
    Table table = null;// w  w w  .  j a  v  a2s  .  c  o  m
    try {
        // Create table if it does not exist yet
        if (!Tables.doesTableExist(dynamoDB, tableName)) {
            System.out.println("Table " + tableName + " is does not exist");
        } else {
            table = dynamo.getTable(tableName);
        }

        // http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.SpecifyingConditions.html
        // select stockTicker, tradeDate, v from stock_eod 
        // where stockTicker = 'LEA' and tradeDate between '02-Oct-2013' and '03-Oct-2013'
        QuerySpec spec = new QuerySpec().withProjectionExpression("stockTicker,tradeDate,v")
                .withKeyConditionExpression("stockTicker = :v_st and tradeDate between :v_sd and :v_ed")
                //               .withFilterExpression("contains(tradeDate, :v_t)")
                .withValueMap(new ValueMap().withString(":v_st", "LEA").withString(":v_sd", "02-Oct-2013")
                        .withString(":v_ed", "03-Oct-2013")
                // .withString(":v_td", "01-Oct-2013")
                //                     .withString(":v_t", "Oct")
                ).withConsistentRead(true);

        ItemCollection<QueryOutcome> items = table.query(spec);

        Iterator<Item> iterator = items.iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next().toJSONPretty());
        }

    } 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());
    }
}