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:org.apache.oodt.cas.filemgr.datatransfer.S3DataTransfererFactory.java

License:Apache License

@Override
public S3DataTransferer createDataTransfer() {
    String bucketName = System.getProperty(BUCKET_NAME_PROPERTY);
    String region = System.getProperty(REGION_PROPERTY);
    String accessKey = System.getProperty(ACCESS_KEY_PROPERTY);
    String secretKey = System.getProperty(SECRET_KEY_PROPERTY);
    boolean encrypt = Boolean.getBoolean(ENCRYPT_PROPERTY);

    AmazonS3Client s3 = new AmazonS3Client(new BasicAWSCredentials(accessKey, secretKey));
    s3.setRegion(Region.getRegion(Regions.valueOf(region)));

    return new S3DataTransferer(s3, bucketName, encrypt);
}

From source file:org.apache.samza.system.kinesis.KinesisConfig.java

License:Apache License

/**
 * Get the Kinesis region for the system stream
 * @param system name of the system// w w w  .ja va2  s . c  o  m
 * @param stream name of the stream
 * @return Kinesis region
 */
Region getRegion(String system, String stream) {
    String name = get(String.format(CONFIG_STREAM_REGION, system, stream),
            get(String.format(CONFIG_SYSTEM_REGION, system)));
    return Region.getRegion(Regions.fromName(name));
}

From source file:org.apache.storm.kinesis.spout.KinesisConnection.java

License:Apache License

void initialize() {
    kinesisClient = new AmazonKinesisClient(kinesisConnectionInfo.getCredentialsProvider(),
            kinesisConnectionInfo.getClientConfiguration());
    kinesisClient.setRegion(Region.getRegion(kinesisConnectionInfo.getRegion()));
}

From source file:org.apache.streams.amazon.kinesis.KinesisPersistReader.java

License:Apache License

@Override
public void prepare(Object configurationObject) {
    // Connect to Kinesis
    synchronized (this) {
        // Create the credentials Object
        AWSCredentials credentials = new BasicAWSCredentials(config.getKey(), config.getSecretKey());

        ClientConfiguration clientConfig = new ClientConfiguration();
        clientConfig.setProtocol(Protocol.valueOf(config.getProtocol().toString()));

        this.client = new AmazonKinesisClient(credentials, clientConfig);
        if (StringUtils.isNotEmpty(config.getRegion()))
            this.client.setRegion(Region.getRegion(Regions.fromName(config.getRegion())));
    }//  ww  w . ja v a 2 s.c o  m
    streamNames = this.config.getStreams();
    executor = Executors.newFixedThreadPool(streamNames.size());
}

From source file:org.apache.streams.amazon.kinesis.KinesisPersistWriter.java

License:Apache License

@Override
public void prepare(Object configurationObject) {
    // Connect to Kinesis
    synchronized (this) {
        // Create the credentials Object
        AWSCredentials credentials = new BasicAWSCredentials(config.getKey(), config.getSecretKey());

        ClientConfiguration clientConfig = new ClientConfiguration();
        clientConfig.setProtocol(Protocol.valueOf(config.getProtocol().toString()));

        this.client = new AmazonKinesisClient(credentials, clientConfig);
        if (StringUtils.isNotEmpty(config.getRegion())) {
            this.client.setRegion(Region.getRegion(Regions.fromName(config.getRegion())));
        }/*w w w . jav a  2  s .c o  m*/
    }
    executor = Executors.newSingleThreadExecutor();

}

From source file:org.apache.streams.s3.S3PersistReader.java

License:Apache License

public void prepare(Object configurationObject) {
    // Connect to S3
    synchronized (this) {
        // Create the credentials Object
        AWSCredentials credentials = new BasicAWSCredentials(s3ReaderConfiguration.getKey(),
                s3ReaderConfiguration.getSecretKey());

        ClientConfiguration clientConfig = new ClientConfiguration();
        clientConfig.setProtocol(Protocol.valueOf(s3ReaderConfiguration.getProtocol().toString()));

        // We do not want path style access
        S3ClientOptions clientOptions = new S3ClientOptions();
        clientOptions.setPathStyleAccess(false);

        this.amazonS3Client = new AmazonS3Client(credentials, clientConfig);
        if (!Strings.isNullOrEmpty(s3ReaderConfiguration.getRegion()))
            this.amazonS3Client
                    .setRegion(Region.getRegion(Regions.fromName(s3ReaderConfiguration.getRegion())));
        this.amazonS3Client.setS3ClientOptions(clientOptions);
    }//w  ww.j  ava 2  s .  com

    final ListObjectsRequest request = new ListObjectsRequest()
            .withBucketName(this.s3ReaderConfiguration.getBucket())
            .withPrefix(s3ReaderConfiguration.getReaderPath()).withMaxKeys(500);

    ObjectListing listing = this.amazonS3Client.listObjects(request);

    this.files = new ArrayList<String>();

    /**
     * If you can list files that are in this path, then you must be dealing with a directory
     * if you cannot list files that are in this path, then you are most likely dealing with
     * a simple file.
     */
    boolean hasCommonPrefixes = listing.getCommonPrefixes().size() > 0 ? true : false;
    boolean hasObjectSummaries = listing.getObjectSummaries().size() > 0 ? true : false;

    if (hasCommonPrefixes || hasObjectSummaries) {
        // Handle the 'directory' use case
        do {
            if (hasCommonPrefixes) {
                for (String file : listing.getCommonPrefixes()) {
                    this.files.add(file);
                }
            } else {
                for (final S3ObjectSummary objectSummary : listing.getObjectSummaries()) {
                    this.files.add(objectSummary.getKey());
                }
            }

            // get the next batch.
            listing = this.amazonS3Client.listNextBatchOfObjects(listing);
        } while (listing.isTruncated());
    } else {
        // handle the single file use-case
        this.files.add(s3ReaderConfiguration.getReaderPath());
    }

    if (this.files.size() <= 0)
        LOGGER.error("There are no files to read");

    this.persistQueue = Queues.synchronizedQueue(new LinkedBlockingQueue<StreamsDatum>(10000));
    this.executor = Executors.newSingleThreadExecutor();
}

From source file:org.apache.streams.s3.S3PersistWriter.java

License:Apache License

public void prepare(Object configurationObject) {
    // Connect to S3
    synchronized (this) {

        try {/*w ww  .j a va  2s.  co m*/
            // if the user has chosen to not set the object mapper, then set a default object mapper for them.
            if (this.objectMapper == null)
                this.objectMapper = new StreamsJacksonMapper();

            // Create the credentials Object
            if (this.amazonS3Client == null) {
                AWSCredentials credentials = new BasicAWSCredentials(s3WriterConfiguration.getKey(),
                        s3WriterConfiguration.getSecretKey());

                ClientConfiguration clientConfig = new ClientConfiguration();
                clientConfig.setProtocol(Protocol.valueOf(s3WriterConfiguration.getProtocol().toString()));

                // We do not want path style access
                S3ClientOptions clientOptions = new S3ClientOptions();
                clientOptions.setPathStyleAccess(false);

                this.amazonS3Client = new AmazonS3Client(credentials, clientConfig);
                if (!Strings.isNullOrEmpty(s3WriterConfiguration.getRegion()))
                    this.amazonS3Client
                            .setRegion(Region.getRegion(Regions.fromName(s3WriterConfiguration.getRegion())));
                this.amazonS3Client.setS3ClientOptions(clientOptions);
            }
        } catch (Exception e) {
            LOGGER.error("Exception while preparing the S3 client: {}", e);
        }

        Preconditions.checkArgument(this.amazonS3Client != null);
    }
}

From source file:org.apache.usergrid.persistence.queue.impl.SNSQueueManagerImpl.java

License:Apache License

private String setupTopics(final String queueName) throws Exception {

    logger.info("Setting up setupTopics SNS/SQS...");

    String primaryTopicArn = AmazonNotificationUtils.getTopicArn(sns, queueName, true);

    if (logger.isTraceEnabled()) {
        logger.trace("SNS/SQS Setup: primaryTopicArn={}", primaryTopicArn);
    }/*from   www.  ja v  a2s  .c  om*/

    String queueUrl = AmazonNotificationUtils.getQueueUrlByName(sqs, queueName);
    String primaryQueueArn = AmazonNotificationUtils.getQueueArnByName(sqs, queueName);

    if (logger.isTraceEnabled()) {
        logger.trace("SNS/SQS Setup: primaryQueueArn={}", primaryQueueArn);
    }

    if (primaryQueueArn == null) {
        if (logger.isTraceEnabled()) {
            logger.trace("SNS/SQS Setup: primaryQueueArn is null, creating queue...");
        }

        queueUrl = AmazonNotificationUtils.createQueue(sqs, queueName, fig);
        primaryQueueArn = AmazonNotificationUtils.getQueueArnByUrl(sqs, queueUrl);

        if (logger.isTraceEnabled()) {
            logger.trace("SNS/SQS Setup: New Queue URL=[{}] ARN=[{}]", queueUrl, primaryQueueArn);
        }
    }

    try {

        SubscribeRequest primarySubscribeRequest = new SubscribeRequest(primaryTopicArn, "sqs",
                primaryQueueArn);
        sns.subscribe(primarySubscribeRequest);

        // ensure the SNS primary topic has permission to send to the primary SQS queue
        List<String> primaryTopicArnList = new ArrayList<>();
        primaryTopicArnList.add(primaryTopicArn);
        AmazonNotificationUtils.setQueuePermissionsToReceive(sqs, queueUrl, primaryTopicArnList);
    } catch (AmazonServiceException e) {
        logger.error("Unable to subscribe PRIMARY queue=[{}] to topic=[{}]", queueUrl, primaryTopicArn, e);
    }

    if (fig.isMultiRegion() && scope.getRegionImplementation() == LegacyQueueScope.RegionImplementation.ALL) {

        String multiRegion = fig.getRegionList();

        if (logger.isTraceEnabled()) {
            logger.trace("MultiRegion Setup specified, regions: [{}]", multiRegion);
        }

        String[] regionNames = multiRegion.split(",");

        final Map<String, String> arrQueueArns = new HashMap<>(regionNames.length + 1);
        final Map<String, String> topicArns = new HashMap<>(regionNames.length + 1);

        arrQueueArns.put(primaryQueueArn, fig.getPrimaryRegion());
        topicArns.put(primaryTopicArn, fig.getPrimaryRegion());

        for (String regionName : regionNames) {

            regionName = regionName.trim();
            Regions regions = Regions.fromName(regionName);
            Region region = Region.getRegion(regions);

            AmazonSQSClient sqsClient = createSQSClient(region);
            AmazonSNSClient snsClient = createSNSClient(region); // do this stuff synchronously

            // getTopicArn will create the SNS topic if it doesn't exist
            String topicArn = AmazonNotificationUtils.getTopicArn(snsClient, queueName, true);
            topicArns.put(topicArn, regionName);

            // create the SQS queue if it doesn't exist
            String queueArn = AmazonNotificationUtils.getQueueArnByName(sqsClient, queueName);
            if (queueArn == null) {
                queueUrl = AmazonNotificationUtils.createQueue(sqsClient, queueName, fig);
                queueArn = AmazonNotificationUtils.getQueueArnByUrl(sqsClient, queueUrl);
            }

            arrQueueArns.put(queueArn, regionName);
        }

        if (logger.isTraceEnabled()) {
            logger.trace("Creating Subscriptions...");
        }

        for (Map.Entry<String, String> queueArnEntry : arrQueueArns.entrySet()) {
            String queueARN = queueArnEntry.getKey();
            String strSqsRegion = queueArnEntry.getValue();

            Regions sqsRegions = Regions.fromName(strSqsRegion);
            Region sqsRegion = Region.getRegion(sqsRegions);

            AmazonSQSClient subscribeSqsClient = createSQSClient(sqsRegion);

            // ensure the URL used to subscribe is for the correct name/region
            String subscribeQueueUrl = AmazonNotificationUtils.getQueueUrlByName(subscribeSqsClient, queueName);

            // this list used later for adding permissions to queues
            List<String> topicArnList = new ArrayList<>();

            for (Map.Entry<String, String> topicArnEntry : topicArns.entrySet()) {

                String topicARN = topicArnEntry.getKey();
                topicArnList.add(topicARN);

                String strSnsRegion = topicArnEntry.getValue();
                Regions snsRegions = Regions.fromName(strSnsRegion);
                Region snsRegion = Region.getRegion(snsRegions);

                AmazonSNSClient subscribeSnsClient = createSNSClient(snsRegion); // do this stuff synchronously
                SubscribeRequest subscribeRequest = new SubscribeRequest(topicARN, "sqs", queueARN);

                try {

                    logger.info("Subscribing Queue ARN/Region=[{} / {}] and Topic ARN/Region=[{} / {}]",
                            queueARN, strSqsRegion, topicARN, strSnsRegion);

                    SubscribeResult subscribeResult = subscribeSnsClient.subscribe(subscribeRequest);
                    String subscriptionARN = subscribeResult.getSubscriptionArn();
                    if (logger.isTraceEnabled()) {
                        logger.trace(
                                "Successfully subscribed Queue ARN=[{}] to Topic ARN=[{}], subscription ARN=[{}]",
                                queueARN, topicARN, subscriptionARN);
                    }
                } catch (Exception e) {
                    logger.error("ERROR Subscribing Queue ARN/Region=[{} / {}] and Topic ARN/Region=[{} / {}]",
                            queueARN, strSqsRegion, topicARN, strSnsRegion, e);
                }
            }

            if (logger.isTraceEnabled()) {
                logger.trace("Adding permission to receive messages...");
            }
            // add permission to each queue, providing a list of topics that it's subscribed to
            AmazonNotificationUtils.setQueuePermissionsToReceive(subscribeSqsClient, subscribeQueueUrl,
                    topicArnList);
        }
    }

    return primaryTopicArn;
}

From source file:org.apache.usergrid.persistence.queue.impl.SNSQueueManagerImpl.java

License:Apache License

/**
 * Get the region
 */
private Region getRegion() {
    Regions regions = Regions.fromName(fig.getPrimaryRegion());
    return Region.getRegion(regions);
}

From source file:org.apache.usergrid.services.assets.data.AWSBinaryStore.java

License:Apache License

private AmazonS3 getS3Client() throws Exception {

    this.bucketName = properties.getProperty("usergrid.binary.bucketname");
    if (bucketName == null) {
        logger.error("usergrid.binary.bucketname not properly set so amazon bucket is null");
        throw new AwsPropertiesNotFoundException("usergrid.binary.bucketname");

    }//from  www .  j  av a  2s.com

    final UsergridAwsCredentialsProvider ugProvider = new UsergridAwsCredentialsProvider();
    AWSCredentials credentials = ugProvider.getCredentials();
    ClientConfiguration clientConfig = new ClientConfiguration();
    clientConfig.setProtocol(Protocol.HTTP);

    s3Client = new AmazonS3Client(credentials, clientConfig);
    if (regionName != null)
        s3Client.setRegion(Region.getRegion(Regions.fromName(regionName)));

    return s3Client;
}