Example usage for com.amazonaws.regions Region getRegion

List of usage examples for com.amazonaws.regions Region getRegion

Introduction

In this page you can find the example usage for com.amazonaws.regions Region getRegion.

Prototype

public static Region getRegion(Regions region) 

Source Link

Document

Returns the region with the id given, or null if it cannot be found in the current regions.xml file.

Usage

From source file:scheduler.SpotRequests.java

License:Open Source License

/**
 * The only information needed to create a client are security credentials
 * consisting of the AWS Access Key ID and Secret Access Key. All other
 * configuration, such as the service endpoints, are performed
 * automatically. Client parameters, such as proxies, can be specified in an
 * optional ClientConfiguration object when constructing a client.
 *
 * @see com.amazonaws.auth.BasicAWSCredentials
 * @see com.amazonaws.auth.PropertiesCredentials
 * @see com.amazonaws.ClientConfiguration
 *//*from  www .  ja  va  2s  .c o m*/
private void init() throws Exception {

    ec2 = new AmazonEC2Client(new ClasspathPropertiesFileCredentialsProvider());
    //      Region usEast1 = Region.getRegion(Regions.US_EAST_1);
    //      ec2.setRegion(usEast1);
    Region usWest = Region.getRegion(Regions.US_WEST_2);
    ec2.setRegion(usWest);
}

From source file:scheduler.SQSService.java

License:Apache License

public SQSService(String queueName) {
    /*/*from www  . j  a  v  a2  s .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);
    }

    sqs = new AmazonSQSClient(credentials);
    Region usEast1 = Region.getRegion(Regions.US_EAST_1);
    sqs.setRegion(usEast1);

    // Create a queue or returns the URL of an existing one
    //System.out.println("Creating a new SQS queue called " + queueName);
    CreateQueueRequest createQueueRequest = new CreateQueueRequest(queueName);
    queueUrl = sqs.createQueue(createQueueRequest).getQueueUrl();

}

From source file:searchengine.WordToDoc.java

License:Open Source License

/**
 * The only information needed to create a client are security credentials
 * consisting of the AWS Access Key ID and Secret Access Key. All other
 * configuration, such as the service endpoints, are performed
 * automatically. Client parameters, such as proxies, can be specified in an
 * optional ClientConfiguration object when constructing a client.
 *
 * @see com.amazonaws.auth.BasicAWSCredentials
 * @see com.amazonaws.auth.ProfilesConfigFile
 * @see com.amazonaws.ClientConfiguration
 *///from w  w  w. j a va2 s.com
private static void init() throws Exception {
    /*
     * The ProfileCredentialsProvider will return your [default]
     * credential profile by reading from the credentials file located at
     * (/home/cis455/.aws/credentials).
     */
    AWSCredentials credentials = null;
    try {
        credentials = new ProfileCredentialsProvider("default").getCredentials();
    } catch (Exception e) {
        throw new AmazonClientException("Cannot load the credentials from the credential profiles file. "
                + "Please make sure that your credentials file is at the correct "
                + "location (/home/cis455/.aws/credentials), and is in valid format.", e);
    }
    dynamoDB = new AmazonDynamoDBClient(credentials);
    Region usWest2 = Region.getRegion(Regions.US_WEST_2);
    dynamoDB.setRegion(usWest2);
}

From source file:Server.aws.yulei.SQSOperation.java

License:Open Source License

public static void sendSQS(String message) {
    AWSCredentials credentials = null;//from  ww w  .ja  va  2  s. co m
    try {
        credentials = new ProfileCredentialsProvider("default").getCredentials();
    } catch (Exception e) {
        throw new AmazonClientException("Cannot load the credentials from the credential profiles file. "
                + "Please make sure that your credentials file is at the correct "
                + "location (/Users/daniel/.aws/credentials), and is in valid format.", e);
    }

    AmazonSQS sqs = new AmazonSQSClient(credentials);
    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("MyQueue");
        String myQueueUrl = "https://sqs.us-east-1.amazonaws.com/659322195879/MyQueue";

        // 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, message));
    } catch (Exception e) {
        Thread.currentThread().interrupt();
    }

}

From source file:Server.aws.yulei.SQSOperation.java

License:Open Source License

public static void receiveSQS() {
    AWSCredentials credentials = null;/*from www  . j a  v  a2  s.c  o m*/
    try {
        credentials = new ProfileCredentialsProvider("default").getCredentials();
    } catch (Exception e) {
        throw new AmazonClientException("Cannot load the credentials from the credential profiles file. "
                + "Please make sure that your credentials file is at the correct "
                + "location (/Users/daniel/.aws/credentials), and is in valid format.", e);
    }

    AmazonSQS sqs = new AmazonSQSClient(credentials);
    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("MyQueue");
        String myQueueUrl = "https://sqs.us-east-1.amazonaws.com/659322195879/MyQueue";

        // List queues

        System.out.println("Receiving messages from MyQueue.\n");
        ReceiveMessageRequest receiveMessageRequest = new ReceiveMessageRequest(myQueueUrl);
        List<Message> messages = sqs.receiveMessage(receiveMessageRequest).getMessages();
        for (Message message : messages) {
            String realData = message.getBody();
            AlchemyAPI alchemyObj = AlchemyAPI
                    .GetInstanceFromString("a47635756e657c0dac0bc2d282dbde377de01513");
            JSONParser parser = new JSONParser();
            JSONObject decodeObj = (JSONObject) parser.parse(realData);
            String content = (String) decodeObj.get("content");

            // load text
            String text = content;

            // analyze text
            Document doc = alchemyObj.TextGetTextSentiment(text);
            String score = "0.00000";
            // parse XML result
            //              String sentiment = doc.getElementsByTagName("type").item(0).getTextContent();
            //                score = doc.getElementsByTagName("score").item(0).getTextContent();
            decodeObj.put("score", score);
            decodeObj.put("sentiment", "Positive");
            StringWriter sw = new StringWriter();
            decodeObj.writeJSONString(sw);

            String jsonText = sw.toString();
            SNSPushToGCM.push(jsonText);
        }

    } catch (Exception e) {
        Thread.currentThread().interrupt();
    }

}

From source file:servlet.FileUploadServlet.java

public void sqs(String msg) throws Exception {
    AWSCredentials awsCreds = new PropertiesCredentials(
            new File("/Users/paulamontojo/Desktop/AwsCredentials.properties"));

    AmazonSQS sqs = new AmazonSQSClient(awsCreds);
    Region usWest2 = Region.getRegion(Regions.US_WEST_2);
    sqs.setRegion(usWest2);// w  w  w . j  a  v  a2 s. c om

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

    try {

        String myQueueUrl = "https://sqs.us-west-2.amazonaws.com/711690152696/MyQueue";

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

        // Send a message
        System.out.println("Sending a message to MyQueue.\n");
        sqs.sendMessage(new SendMessageRequest(myQueueUrl, msg));

    } 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:solutiontogo.de.audiocitytourguide.utils.AmazonS3Utility.java

License:Open Source License

/**
 * Gets an instance of a S3 client which is constructed using the given
 * Context./*from  w  w  w  .  ja  v a  2 s .  c om*/
 *
 * @param context An Context instance.
 * @return A default S3 client.
 */
public static AmazonS3Client getS3Client(Context context) {
    if (sS3Client == null) {
        sS3Client = new AmazonS3Client(getCredProvider(context.getApplicationContext()));
        sS3Client.setRegion(Region.getRegion(Regions.US_WEST_2));
    }
    return sS3Client;
}

From source file:sqs_examples.SimpleQueueServiceSample.java

License:Open Source License

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

    /*//from w w w .j av  a 2 s  . com
     * The ProfileCredentialsProvider will return your [default]
     * credential profile by reading from the credentials file located at
     * (/Users/pmacharl/.aws/credentials).
     */
    AWSCredentials credentials = null;
    try {
        credentials = new ProfileCredentialsProvider("default").getCredentials();
    } catch (Exception e) {
        throw new AmazonClientException("Cannot load the credentials from the credential profiles file. "
                + "Please make sure that your credentials file is at the correct "
                + "location (/Users/pmacharl/.aws/credentials), and is in valid format.", e);
    }

    AmazonSQS sqs = new AmazonSQSClient(credentials);
    Region usWest2 = Region.getRegion(Regions.US_WEST_2);
    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("MyQueue");
        String myQueueUrl = sqs.createQueue(createQueueRequest).getQueueUrl();

        // 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:squash.booking.lambdas.core.BackupManager.java

License:Apache License

@Override
public final void initialise(IBookingManager bookingManager, IRuleManager ruleManager, LambdaLogger logger)
        throws Exception {
    this.ruleManager = ruleManager;
    this.bookingManager = bookingManager;
    this.logger = logger;
    databaseBackupBucketName = getEnvironmentVariable("DatabaseBackupBucket");
    adminSnsTopicArn = getEnvironmentVariable("AdminSNSTopicArn");
    region = Region.getRegion(Regions.fromName(getEnvironmentVariable("AWS_REGION")));

    // Prepare to serialise bookings and booking rules as JSON.
    mapper = new ObjectMapper();
    mapper.setSerializationInclusion(Include.NON_EMPTY);
    mapper.setSerializationInclusion(Include.NON_NULL);

    initialised = true;// w  w  w .  j a va  2  s .c  o  m
}

From source file:squash.booking.lambdas.core.BookingManager.java

License:Apache License

@Override
public final void initialise(LambdaLogger logger) throws Exception {
    this.logger = logger;
    adminSnsTopicArn = getEnvironmentVariable("AdminSNSTopicArn");
    region = Region.getRegion(Regions.fromName(getEnvironmentVariable("AWS_REGION")));
    initialised = true;/*www.  j  a va 2 s  . c o  m*/
}