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.zeppelin.notebook.repo.OldS3NotebookRepo.java

License:Apache License

public void init(ZeppelinConfiguration conf) throws IOException {
    this.conf = conf;
    bucketName = conf.getS3BucketName();
    user = conf.getS3User();//  w  w  w  .j a v a2  s  .c om
    useServerSideEncryption = conf.isS3ServerSideEncryption();

    // always use the default provider chain
    AWSCredentialsProvider credentialsProvider = new DefaultAWSCredentialsProviderChain();
    CryptoConfiguration cryptoConf = new CryptoConfiguration();
    String keyRegion = conf.getS3KMSKeyRegion();

    if (StringUtils.isNotBlank(keyRegion)) {
        cryptoConf.setAwsKmsRegion(Region.getRegion(Regions.fromName(keyRegion)));
    }

    ClientConfiguration cliConf = createClientConfiguration();

    // see if we should be encrypting data in S3
    String kmsKeyID = conf.getS3KMSKeyID();
    if (kmsKeyID != null) {
        // use the AWS KMS to encrypt data
        KMSEncryptionMaterialsProvider emp = new KMSEncryptionMaterialsProvider(kmsKeyID);
        this.s3client = new AmazonS3EncryptionClient(credentialsProvider, emp, cliConf, cryptoConf);
    } else if (conf.getS3EncryptionMaterialsProviderClass() != null) {
        // use a custom encryption materials provider class
        EncryptionMaterialsProvider emp = createCustomProvider(conf);
        this.s3client = new AmazonS3EncryptionClient(credentialsProvider, emp, cliConf, cryptoConf);
    } else {
        // regular S3
        this.s3client = new AmazonS3Client(credentialsProvider, cliConf);
    }

    // set S3 endpoint to use
    s3client.setEndpoint(conf.getS3Endpoint());
}

From source file:org.apache.zeppelin.notebook.repo.S3NotebookRepo.java

License:Apache License

public void init(ZeppelinConfiguration conf) throws IOException {
    this.conf = conf;
    bucketName = conf.getS3BucketName();
    user = conf.getS3User();//from  w  w w  .  ja va 2  s  .co  m
    rootFolder = user + "/notebook";
    useServerSideEncryption = conf.isS3ServerSideEncryption();

    // always use the default provider chain
    AWSCredentialsProvider credentialsProvider = new DefaultAWSCredentialsProviderChain();
    CryptoConfiguration cryptoConf = new CryptoConfiguration();
    String keyRegion = conf.getS3KMSKeyRegion();

    if (StringUtils.isNotBlank(keyRegion)) {
        cryptoConf.setAwsKmsRegion(Region.getRegion(Regions.fromName(keyRegion)));
    }

    ClientConfiguration cliConf = createClientConfiguration();

    // see if we should be encrypting data in S3
    String kmsKeyID = conf.getS3KMSKeyID();
    if (kmsKeyID != null) {
        // use the AWS KMS to encrypt data
        KMSEncryptionMaterialsProvider emp = new KMSEncryptionMaterialsProvider(kmsKeyID);
        this.s3client = new AmazonS3EncryptionClient(credentialsProvider, emp, cliConf, cryptoConf);
    } else if (conf.getS3EncryptionMaterialsProviderClass() != null) {
        // use a custom encryption materials provider class
        EncryptionMaterialsProvider emp = createCustomProvider(conf);
        this.s3client = new AmazonS3EncryptionClient(credentialsProvider, emp, cliConf, cryptoConf);
    } else {
        // regular S3
        this.s3client = new AmazonS3Client(credentialsProvider, cliConf);
    }

    // set S3 endpoint to use
    s3client.setEndpoint(conf.getS3Endpoint());
}

From source file:org.cloudifysource.esc.driver.provisioning.privateEc2.RegionUtils.java

License:Open Source License

/**
 * Convert an availability zone to com.amazonaws.regions.Region.
 * /* ww w.  j  a  va  2s  .  c  o  m*/
 * @param availabilityZone
 *            The availability zone to convert (i.e. us-east-1a, us-east-1b, ...).
 * @return The converted com.amazonaws.regions.Region.
 */
public static Region convertAvailabilityZone2Region(final String availabilityZone) {
    Region region;
    String regionStr = availabilityZone;
    String lastChar = availabilityZone.substring(availabilityZone.length() - 1, availabilityZone.length());
    if (!NumberUtils.isDigits(lastChar)) {
        regionStr = availabilityZone.substring(0, availabilityZone.length() - 1);

    }
    region = Region.getRegion(Regions.valueOf(regionStr.replaceAll("-", "_").toUpperCase()));
    return region;
}

From source file:org.cloudifysource.esc.driver.provisioning.privateEc2.RegionUtils.java

License:Open Source License

/**
 * Convert a location id to com.amazonaws.regions.Region.
 * /* w w  w  .  j  av a  2s  . c  om*/
 * @param locationId
 *            The location id to convert (i.e. us-east-1)
 * @return The converted com.amazonaws.regions.Region.
 */
public static Region convertLocationId2Region(final String locationId) {
    String regionString = locationId.replaceAll("-", "_").toUpperCase();
    Regions regionEnum = Regions.valueOf(regionString);
    Region region = Region.getRegion(regionEnum);
    return region;
}

From source file:org.cto.VVS3Box.S3Sample.java

License:Open Source License

public static void main(String[] args) throws IOException {
    /*//from   w ww.j  a v a  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.US_WEST_2);
    s3.setRegion(usWest2);

    String bucketName = "lior.test-" + 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:org.dspace.storage.bitstore.S3BitStoreService.java

License:BSD License

/**
 * Initialize the asset store/*  w w w.j  a v  a 2  s  . c o  m*/
 * S3 Requires:
 *  - access key
 *  - secret key
 *  - bucket name
 */
public void init() throws IOException {
    if (StringUtils.isBlank(getAwsAccessKey()) || StringUtils.isBlank(getAwsSecretKey())) {
        log.warn("Empty S3 access or secret");
    }

    // init client
    AWSCredentials awsCredentials = new BasicAWSCredentials(getAwsAccessKey(), getAwsSecretKey());
    s3Service = new AmazonS3Client(awsCredentials);

    // bucket name
    if (StringUtils.isEmpty(bucketName)) {
        bucketName = "dspace-asset-" + ConfigurationManager.getProperty("dspace.hostname");
        log.warn("S3 BucketName is not configured, setting default: " + bucketName);
    }

    try {
        if (!s3Service.doesBucketExist(bucketName)) {
            s3Service.createBucket(bucketName);
            log.info("Creating new S3 Bucket: " + bucketName);
        }
    } catch (Exception e) {
        log.error(e);
        throw new IOException(e);
    }

    // region
    if (StringUtils.isNotBlank(awsRegionName)) {
        try {
            Regions regions = Regions.fromName(awsRegionName);
            Region region = Region.getRegion(regions);
            s3Service.setRegion(region);
            log.info("S3 Region set to: " + region.getName());
        } catch (IllegalArgumentException e) {
            log.warn("Invalid aws_region: " + awsRegionName);
        }
    }

    log.info("AWS S3 Assetstore ready to go! bucket:" + bucketName);
}

From source file:org.dspace.storage.bitstore.S3BitStoreService.java

License:BSD License

/**
 * Contains a command-line testing tool. Expects arguments:
 *  -a accessKey -s secretKey -f assetFileName
 *
 * @param args/*w  w w.j a va  2  s .c  om*/
 *        Command line arguments
 */
public static void main(String[] args) throws Exception {
    //TODO use proper CLI, or refactor to be a unit test. Can't mock this without keys though.

    // parse command line
    String assetFile = null;
    String accessKey = null;
    String secretKey = null;

    for (int i = 0; i < args.length; i += 2) {
        if (args[i].startsWith("-a")) {
            accessKey = args[i + 1];
        } else if (args[i].startsWith("-s")) {
            secretKey = args[i + 1];
        } else if (args[i].startsWith("-f")) {
            assetFile = args[i + 1];
        }
    }

    if (accessKey == null || secretKey == null || assetFile == null) {
        System.out.println("Missing arguments - exiting");
        return;
    }
    S3BitStoreService store = new S3BitStoreService();

    AWSCredentials awsCredentials = new BasicAWSCredentials(accessKey, secretKey);

    store.s3Service = new AmazonS3Client(awsCredentials);

    //Todo configurable region
    Region usEast1 = Region.getRegion(Regions.US_EAST_1);
    store.s3Service.setRegion(usEast1);

    //Bucketname should be lowercase
    store.bucketName = "dspace-asset-" + ConfigurationManager.getProperty("dspace.hostname") + ".s3test";
    store.s3Service.createBucket(store.bucketName);
    /* Broken in DSpace 6 TODO Refactor
            // time everything, todo, swtich to caliper
            long start = System.currentTimeMillis();
            // Case 1: store a file
            String id = store.generateId();
            System.out.print("put() file " + assetFile + " under ID " + id + ": ");
            FileInputStream fis = new FileInputStream(assetFile);
            //TODO create bitstream for assetfile...
            Map attrs = store.put(fis, id);
            long now =  System.currentTimeMillis();
            System.out.println((now - start) + " msecs");
            start = now;
            // examine the metadata returned
            Iterator iter = attrs.keySet().iterator();
            System.out.println("Metadata after put():");
            while (iter.hasNext())
            {
               String key = (String)iter.next();
               System.out.println( key + ": " + (String)attrs.get(key) );
            }
            // Case 2: get metadata and compare
            System.out.print("about() file with ID " + id + ": ");
            Map attrs2 = store.about(id, attrs);
            now =  System.currentTimeMillis();
            System.out.println((now - start) + " msecs");
            start = now;
            iter = attrs2.keySet().iterator();
            System.out.println("Metadata after about():");
            while (iter.hasNext())
            {
               String key = (String)iter.next();
               System.out.println( key + ": " + (String)attrs.get(key) );
            }
            // Case 3: retrieve asset and compare bits
            System.out.print("get() file with ID " + id + ": ");
            java.io.FileOutputStream fos = new java.io.FileOutputStream(assetFile+".echo");
            InputStream in = store.get(id);
            Utils.bufferedCopy(in, fos);
            fos.close();
            in.close();
            now =  System.currentTimeMillis();
            System.out.println((now - start) + " msecs");
            start = now;
            // Case 4: remove asset
            System.out.print("remove() file with ID: " + id + ": ");
            store.remove(id);
            now =  System.currentTimeMillis();
            System.out.println((now - start) + " msecs");
            System.out.flush();
            // should get nothing back now - will throw exception
            store.get(id);
    */
}

From source file:org.freeeed.aws.SimpleQueueServiceSample.java

License:Apache License

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

    /*//ww w  . j a v a  2 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);
    }

    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");
        // TODO restore
        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.toString());
        }
        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:org.gradle.internal.resource.transport.aws.s3.S3RegionalResource.java

License:Apache License

private void configure() {
    Matcher matcher = REGIONAL_ENDPOINT_PATTERN.matcher(uri.toString());
    if (matcher.find()) {
        String bucketName = matcher.group(1);
        String region = matcher.group(2);
        String key = matcher.group(4);
        Region derivedRegion;/*from w  ww.  j  a v a  2 s. c  om*/
        if (region.equals("external-1")) {
            derivedRegion = Region.getRegion(Regions.US_EAST_1);
        } else {
            derivedRegion = RegionUtils.getRegion(region);
        }

        this.region = derivedRegion;
        this.bucketName = bucketName;
        this.key = key;
    } else {
        this.region = DEFAULT_REGION;
        this.bucketName = getBucketName(uri.getHost());
        this.key = getS3BucketKey(uri);
    }
}

From source file:org.hashbang.util.AutoDiscoverQueue.java

License:Open Source License

private static AmazonSQS init() {

    /*/*from  ww w. j a  v a2s  . 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);
    }

    AmazonSQS sqs = new AmazonSQSClient(credentials);
    Region usWest2 = Region.getRegion(Regions.US_WEST_2);
    sqs.setRegion(usWest2);
    return sqs;
}