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:com.dell.doradus.db.dynamodb.DynamoDBService.java

License:Apache License

private void setRegionOrEndPoint() {
    String regionName = getParamString("ddb_region");
    if (regionName != null) {
        Regions regionEnum = Regions.fromName(regionName);
        Utils.require(regionEnum != null, "Unknown 'ddb_region': " + regionName);
        m_logger.info("Using region: {}", regionName);
        m_ddbClient.setRegion(Region.getRegion(regionEnum));
    } else {//from  w w w .  j a v a2s .c  o m
        String ddbEndpoint = getParamString("ddb_endpoint");
        Utils.require(ddbEndpoint != null,
                "Either 'ddb_region' or 'ddb_endpoint' must be defined for tenant: " + m_tenant.getName());
        m_logger.info("Using endpoint: {}", ddbEndpoint);
        m_ddbClient.setEndpoint(ddbEndpoint);
    }
}

From source file:com.dev.appx.sns.SNSMobilePush.java

License:Open Source License

public void createTopic(String topic) throws IOException {
    //create a new SNS client and set endpoint
    AmazonSNSClient snsClient = new AmazonSNSClient(new ClasspathPropertiesFileCredentialsProvider());

    /*AmazonSNS snsClient = new AmazonSNSClient(new PropertiesCredentials(
    SNSMobilePush.class/*from  w  w w  . j a v a 2 s  .  c om*/
          .getResourceAsStream("AwsCredentials.properties")));*/

    snsClient.setRegion(Region.getRegion(Regions.US_EAST_1));

    //create a new SNS topic
    CreateTopicRequest createTopicRequest = new CreateTopicRequest(topic);
    CreateTopicResult createTopicResult = snsClient.createTopic(createTopicRequest);
    //print TopicArn
    System.out.println(createTopicResult);
    //get request id for CreateTopicRequest from SNS metadata      
    System.out.println("CreateTopicRequest - " + snsClient.getCachedResponseMetadata(createTopicRequest));
}

From source file:com.devnexus.ting.core.service.integration.AmazonSesSender.java

License:Apache License

public void sendUsingSendgrid(GenericEmail email) {

    final Destination destination = new Destination(email.getTo());
    destination.setCcAddresses(email.getCc());

    final Content subject = new Content(email.getSubject());
    final Content htmlContent = new Content().withData(email.getHtml());
    final Content textContent = new Content().withData(email.getText());

    final Body body = new Body().withHtml(htmlContent).withText(textContent);

    final Message message = new Message().withSubject(subject).withBody(body);

    final SendEmailRequest request = new SendEmailRequest().withSource(email.getFrom())
            .withDestination(destination).withMessage(message);

    final Region REGION = Region.getRegion(Regions.US_EAST_1);
    client.setRegion(REGION);/* ww w. ja v  a  2s. com*/

    // Send the email.
    client.sendEmail(request);
    LOGGER.info("Email sent to {}", email.getTo());

}

From source file:com.dushyant.flume.sink.aws.sqs.BasicSQSMsgSender.java

License:Apache License

public BasicSQSMsgSender(String sqsUrl, String region, String awsAccessKey, String awsSecretKey) {
    this.sqsUrl = sqsUrl;
    this.region = region;
    this.awsAccessKey = awsAccessKey;
    this.awsSecretKey = awsSecretKey;

    AmazonSQS sqs = null;//  ww w  . j a  va 2  s. co m
    if (StringUtils.isBlank(awsAccessKey) || StringUtils.isBlank(awsSecretKey)) {
        LOG.warn(
                "Either awsAccessKey or awsSecretKey not specified. Will use DefaultAWSCredentialsProviderChain "
                        + "to look for AWS credentials.");
        sqs = new AmazonSQSClient();
    } else {
        sqs = new AmazonSQSClient(new BasicAWSCredentials(this.awsAccessKey, this.awsSecretKey));
    }

    Region sqsRegion = Region.getRegion(Regions.fromName(this.region));
    sqs.setRegion(sqsRegion);

    this.amazonSQS = sqs;
}

From source file:com.dushyant.flume.sink.aws.sqs.BatchSQSMsgSender.java

License:Apache License

public BatchSQSMsgSender(String sqsUrl, String region, String awsAccessKey, String awsSecretKey, int batchSize,
        long maxMessageSize) {
    this.sqsUrl = sqsUrl;
    this.region = region;
    this.awsAccessKey = awsAccessKey;
    this.awsSecretKey = awsSecretKey;

    AmazonSQS sqs = null;//www . j  a  v  a  2  s . com
    if (StringUtils.isBlank(awsAccessKey) || StringUtils.isBlank(awsSecretKey)) {
        LOG.warn(
                "Either awsAccessKey or awsSecretKey not specified. Will use DefaultAWSCredentialsProviderChain "
                        + "to look for AWS credentials.");
        sqs = new AmazonSQSClient();
    } else {
        sqs = new AmazonSQSClient(new BasicAWSCredentials(this.awsAccessKey, this.awsSecretKey));
    }

    Region sqsRegion = Region.getRegion(Regions.fromName(this.region));
    sqs.setRegion(sqsRegion);

    this.batchSize = batchSize;
    this.maxMessageSize = maxMessageSize;

    this.amazonSQS = sqs;
}

From source file:com.dustindoloff.s3websitedeploy.Main.java

License:Apache License

private static Region getBucketRegion(final AmazonS3 s3Client, final String bucket) {
    try {//ww  w .java2  s .co m
        return Region.getRegion(Regions.fromName(s3Client.getBucketLocation(bucket)));
    } catch (final AmazonClientException | IllegalArgumentException e) {
        return null;
    }
}

From source file:com.dxc.temp.SimpleQueueServiceSample.java

License:Open Source License

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

    BasicConfigurator.configure();//w ww . ja  v a2  s.c  o m

    /*
     * The ProfileCredentialsProvider will return your [default]
     * credential profile by reading from the credentials file located at
     * (~/.aws/credentials).
     */
    AWSCredentials credentials = null;
    try {
        credentials = new ProfileCredentialsProvider().getCredentials();
    } catch (Exception e) {
        throw new AmazonClientException("Cannot load the credentials from the credential profiles file. "
                + "Please make sure that your credentials file is at the correct "
                + "location (~/.aws/credentials), and is in valid format.", e);
    }
    System.out.println(String.format("Found AWSAccessKeyId: %s", credentials.getAWSAccessKeyId()));
    System.out.println(String.format("Found AWSAccessSecretKey: %s", credentials.getAWSSecretKey()));

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

    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, "Message body 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
        // You must wait 60 seconds after deleting a queue before you can create another with the same name
        //            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.dynamo.DynamoData.java

License:Open Source License

public void init() throws Exception {

    AWSCredentials credentials = null;/*  w ww . ja v  a  2  s  .com*/
    try {
        credentials = new BasicAWSCredentials("AKIAI6QKTRAQE7MXQOIQ",
                "wIG6u1yI5ZaseeJbvYSUmD98qelIJNSCVBzt5k2q");
    } 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 (C:\\Users\\bssan_000\\.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:com.easarrive.image.thumbor.executer.impl.AbstractSQSServer.java

License:Open Source License

@Override
public void init() {
    this.setServerStatus(Status.INIT);
    try {/*from w  w w.  j  av a2s. c  o  m*/
        // 
        String regionString = PropertyConfigurer.getPropertyValue(Constant.SQS.Property.REGION.getKey());
        if (StringUtil.isEmpty(regionString)) {
            throw new ThumborException("SQS region is null or empty");
        }
        Regions regions = Regions.fromName(regionString);
        Region region = Region.getRegion(regions);

        // SQS??
        String queueUrlString = PropertyConfigurer.getPropertyValue(Constant.SQS.Property.QUEUE_URL.getKey());
        if (StringUtil.isEmpty(queueUrlString)) {
            throw new ThumborException("SQS queueUrl is null or empty");
        }

        // ??
        String maxNumberOfMessagesString = PropertyConfigurer
                .getPropertyValue(Constant.SQS.Property.MAX_NUMBER_OF_MESSAGES.getKey());
        if (StringUtil.isEmpty(maxNumberOfMessagesString) || !maxNumberOfMessagesString.matches("^\\d+$")) {
            maxNumberOfMessagesString = Constant.SQS.Property.MAX_NUMBER_OF_MESSAGES.getValue();
        }
        Integer maxNumberOfMessages = Integer.valueOf(maxNumberOfMessagesString);

        // ???
        String maxThreadOfMessagesString = PropertyConfigurer
                .getPropertyValue(Constant.SQS.Property.MAX_THREAD_OF_MESSAGES.getKey());
        if (StringUtil.isEmpty(maxThreadOfMessagesString) || !maxThreadOfMessagesString.matches("^\\d+$")) {
            maxThreadOfMessagesString = Constant.SQS.Property.MAX_THREAD_OF_MESSAGES.getValue();
        }
        Integer maxThreadCountRequestSQS = Integer.valueOf(maxThreadOfMessagesString);

        // SQS ????(??s)
        String visibilityTimeoutString = PropertyConfigurer
                .getPropertyValue(Constant.SQS.Property.VISIBILITY_TIMEOUT.getKey());
        if (StringUtil.isEmpty(maxThreadOfMessagesString) || !maxThreadOfMessagesString.matches("^\\d+$")) {
            visibilityTimeoutString = Constant.SQS.Property.VISIBILITY_TIMEOUT.getValue();
        }
        Integer visibilityTimeout = Integer.valueOf(visibilityTimeoutString);

        this.region = region;
        this.queueUrl = queueUrlString;
        this.maxNumberOfMessages = maxNumberOfMessages;
        this.maxThreadCountRequestSQS = maxThreadCountRequestSQS;
        this.visibilityTimeout = visibilityTimeout;
        this.setServerStatus(this.getServerStatus(), Status.STARTING);
    } catch (Exception e) {
        if (logger.isErrorEnabled()) {
            logger.error(e.getMessage(), e);
        }
        System.exit(0);
    }
}

From source file:com.easarrive.image.thumbor.executer.service.impl.SQSNotificationHandlerForThumborCallable.java

License:Open Source License

private Region getRegion(S3EventMessageRecord snsMessageRecord) throws Exception {
    String awsRegion = snsMessageRecord.getAwsRegion();
    Regions regions = Regions.fromName(awsRegion);
    Region region = Region.getRegion(regions);
    return region;
}