Example usage for com.amazonaws.regions Regions getCurrentRegion

List of usage examples for com.amazonaws.regions Regions getCurrentRegion

Introduction

In this page you can find the example usage for com.amazonaws.regions Regions getCurrentRegion.

Prototype

public static Region getCurrentRegion() 

Source Link

Document

Returns a Region object representing the region the application is running in, when running in EC2.

Usage

From source file:com.bodybuilding.turbine.discovery.AsgTagInstanceDiscovery.java

License:Apache License

protected AsgTagInstanceDiscovery(AmazonAutoScalingClient asgClient, AmazonEC2Client ec2Client) {
    Preconditions.checkNotNull(asgClient);
    Preconditions.checkNotNull(ec2Client);
    Preconditions.checkState(!Strings.isNullOrEmpty(CLUSTER_TAG_KEY.get()),
            TAG_PROPERTY_NAME + " must be supplied!");
    this.asgClient = asgClient;
    this.ec2Client = ec2Client;

    String regionName = DynamicPropertyFactory.getInstance().getStringProperty("turbine.region", "").get();
    if (Strings.isNullOrEmpty(regionName)) {
        Region currentRegion = Regions.getCurrentRegion();
        if (currentRegion != null) {
            regionName = currentRegion.getName();
        } else {//  w  w  w.jav a  2 s .  c o m
            regionName = "us-east-1";
        }
    }
    Region region = Region.getRegion(Regions.fromName(regionName));
    ec2Client.setRegion(region);
    asgClient.setRegion(region);
    log.debug("Set the region to [{}]", region);
}

From source file:com.bodybuilding.turbine.discovery.Ec2TagInstanceDiscovery.java

License:Apache License

protected Ec2TagInstanceDiscovery(AmazonEC2Client ec2Client) {
    Preconditions.checkNotNull(ec2Client);
    this.ec2Client = ec2Client;

    Preconditions.checkState(!Strings.isNullOrEmpty(CLUSTER_TAG_KEY.get()),
            PROPERTY_NAME + " must be supplied!");
    String regionName = DynamicPropertyFactory.getInstance().getStringProperty("turbine.region", "").get();
    if (Strings.isNullOrEmpty(regionName)) {
        Region currentRegion = Regions.getCurrentRegion();
        if (currentRegion != null) {
            regionName = currentRegion.getName();
        } else {//from  w  ww.  ja  va 2 s. co  m
            regionName = "us-east-1";
        }
    }
    ec2Client.setRegion(Region.getRegion(Regions.fromName(regionName)));
    log.debug("Set the ec2 region to [{}]", regionName);
}

From source file:com.erudika.para.iot.AWSIoTService.java

License:Apache License

protected AWSIotClient getClient() {
    if (iotClient != null) {
        return iotClient;
    }/*from   w  w w .  j av a  2 s .  c o m*/

    Region region = Regions.getCurrentRegion();
    region = region != null ? region : Region.getRegion(Regions.fromName(Config.AWS_REGION));
    iotClient = new AWSIotClient(new BasicAWSCredentials(Config.AWS_ACCESSKEY, Config.AWS_SECRETKEY))
            .withRegion(region);

    Para.addDestroyListener(new DestroyListener() {
        public void onDestroy() {
            shutdownClient();
        }
    });

    return iotClient;
}

From source file:com.erudika.para.iot.AWSIoTService.java

License:Apache License

protected AWSIotDataClient getDataClient() {
    if (iotDataClient != null) {
        return iotDataClient;
    }/*from   w w w .  java 2s . c o m*/

    Region region = Regions.getCurrentRegion();
    region = region != null ? region : Region.getRegion(Regions.fromName(Config.AWS_REGION));
    iotDataClient = new AWSIotDataClient(new BasicAWSCredentials(Config.AWS_ACCESSKEY, Config.AWS_SECRETKEY))
            .withRegion(region);

    Para.addDestroyListener(new DestroyListener() {
        public void onDestroy() {
            shutdownDataClient();
        }
    });

    return iotDataClient;
}

From source file:com.erudika.para.persistence.AWSDynamoUtils.java

License:Apache License

/**
 * Returns a client instance for AWS DynamoDB.
 * @return a client that talks to DynamoDB
 *///from w w w  . j  a  v a2 s .co  m
public static AmazonDynamoDBClient getClient() {
    if (ddbClient != null) {
        return ddbClient;
    }

    if (Config.IN_PRODUCTION) {
        Region region = Regions.getCurrentRegion();
        region = region != null ? region : Region.getRegion(Regions.fromName(Config.AWS_REGION));
        ddbClient = new AmazonDynamoDBClient(
                new BasicAWSCredentials(Config.AWS_ACCESSKEY, Config.AWS_SECRETKEY)).withRegion(region);

    } else {
        ddbClient = new AmazonDynamoDBClient(new BasicAWSCredentials("local", "null"))
                .withEndpoint(LOCAL_ENDPOINT);
    }

    if (!existsTable(Config.APP_NAME_NS)) {
        createTable(Config.APP_NAME_NS);
    }

    ddb = new DynamoDB(ddbClient);

    Para.addDestroyListener(new DestroyListener() {
        public void onDestroy() {
            shutdownClient();
        }
    });

    return ddbClient;
}

From source file:com.erudika.para.queue.AWSQueueUtils.java

License:Apache License

/**
 * Returns a client instance for AWS SQS.
 * @return a client that talks to SQS/*w w  w.j a  v a2  s.  com*/
 */
public static AmazonSQSClient getClient() {
    if (sqsClient != null) {
        return sqsClient;
    }
    if (Config.IN_PRODUCTION) {
        Region region = Regions.getCurrentRegion();
        region = region != null ? region : Region.getRegion(Regions.fromName(Config.AWS_REGION));
        sqsClient = new AmazonSQSClient(new BasicAWSCredentials(Config.AWS_ACCESSKEY, Config.AWS_SECRETKEY))
                .withRegion(region);
    } else {
        sqsClient = new AmazonSQSClient(new BasicAWSCredentials("x", "x")).withEndpoint(LOCAL_ENDPOINT);
    }

    Para.addDestroyListener(new DestroyListener() {
        public void onDestroy() {
            sqsClient.shutdown();
        }
    });
    return sqsClient;
}

From source file:com.erudika.para.storage.AWSFileStore.java

License:Apache License

/**
 * Creates a new instance based on the bucket provided.
 * @param bucket the name of the S3 bucket
 *//*www .j av  a2  s.c  o m*/
public AWSFileStore(String bucket) {
    this.bucket = bucket;
    this.awsCredentials = new BasicAWSCredentials(Config.AWS_ACCESSKEY, Config.AWS_SECRETKEY);
    Region region = Regions.getCurrentRegion();
    region = region != null ? region : Region.getRegion(Regions.fromName(Config.AWS_REGION));
    this.s3 = new AmazonS3Client(awsCredentials).withRegion(region);
}

From source file:com.facebook.presto.hive.metastore.glue.GlueHiveMetastore.java

License:Apache License

private static AWSGlueAsync createAsyncGlueClient(GlueHiveMetastoreConfig config) {
    ClientConfiguration clientConfig = new ClientConfiguration()
            .withMaxConnections(config.getMaxGlueConnections());
    AWSGlueAsyncClientBuilder asyncGlueClientBuilder = AWSGlueAsyncClientBuilder.standard()
            .withClientConfiguration(clientConfig);

    if (config.getGlueRegion().isPresent()) {
        asyncGlueClientBuilder.setRegion(config.getGlueRegion().get());
    } else if (config.getPinGlueClientToCurrentRegion()) {
        Region currentRegion = Regions.getCurrentRegion();
        if (currentRegion != null) {
            asyncGlueClientBuilder.setRegion(currentRegion.getName());
        }//from w  w  w.j av  a  2  s . c  o  m
    }

    return asyncGlueClientBuilder.build();
}

From source file:com.facebook.presto.hive.s3.PrestoS3ClientFactory.java

License:Apache License

synchronized AmazonS3 getS3Client(Configuration config, HiveClientConfig clientConfig) {
    if (s3Client != null) {
        return s3Client;
    }// w w  w .j a  v  a 2  s.  c  o  m

    HiveS3Config defaults = new HiveS3Config();
    String userAgentPrefix = config.get(S3_USER_AGENT_PREFIX, defaults.getS3UserAgentPrefix());
    int maxErrorRetries = config.getInt(S3_MAX_ERROR_RETRIES, defaults.getS3MaxErrorRetries());
    boolean sslEnabled = config.getBoolean(S3_SSL_ENABLED, defaults.isS3SslEnabled());
    Duration connectTimeout = Duration
            .valueOf(config.get(S3_CONNECT_TIMEOUT, defaults.getS3ConnectTimeout().toString()));
    Duration socketTimeout = Duration
            .valueOf(config.get(S3_SOCKET_TIMEOUT, defaults.getS3SocketTimeout().toString()));
    int maxConnections = config.getInt(S3_SELECT_PUSHDOWN_MAX_CONNECTIONS,
            clientConfig.getS3SelectPushdownMaxConnections());

    if (clientConfig.isS3SelectPushdownEnabled()) {
        s3UserAgentSuffix = "presto-select";
    }

    ClientConfiguration clientConfiguration = new ClientConfiguration().withMaxErrorRetry(maxErrorRetries)
            .withProtocol(sslEnabled ? Protocol.HTTPS : Protocol.HTTP)
            .withConnectionTimeout(toIntExact(connectTimeout.toMillis()))
            .withSocketTimeout(toIntExact(socketTimeout.toMillis())).withMaxConnections(maxConnections)
            .withUserAgentPrefix(userAgentPrefix).withUserAgentSuffix(s3UserAgentSuffix);

    PrestoS3FileSystemStats stats = new PrestoS3FileSystemStats();
    RequestMetricCollector metricCollector = new PrestoS3FileSystemMetricCollector(stats);
    AWSCredentialsProvider awsCredentialsProvider = getAwsCredentialsProvider(config, defaults);
    AmazonS3Builder<? extends AmazonS3Builder, ? extends AmazonS3> clientBuilder = AmazonS3Client.builder()
            .withCredentials(awsCredentialsProvider).withClientConfiguration(clientConfiguration)
            .withMetricsCollector(metricCollector).enablePathStyleAccess();

    boolean regionOrEndpointSet = false;

    String endpoint = config.get(S3_ENDPOINT);
    boolean pinS3ClientToCurrentRegion = config.getBoolean(S3_PIN_CLIENT_TO_CURRENT_REGION,
            defaults.isPinS3ClientToCurrentRegion());
    verify(!pinS3ClientToCurrentRegion || endpoint == null,
            "Invalid configuration: either endpoint can be set or S3 client can be pinned to the current region");

    // use local region when running inside of EC2
    if (pinS3ClientToCurrentRegion) {
        Region region = Regions.getCurrentRegion();
        if (region != null) {
            clientBuilder.withRegion(region.getName());
            regionOrEndpointSet = true;
        }
    }

    if (!isNullOrEmpty(endpoint)) {
        clientBuilder.withEndpointConfiguration(new EndpointConfiguration(endpoint, null));
        regionOrEndpointSet = true;
    }

    if (!regionOrEndpointSet) {
        clientBuilder.withRegion(US_EAST_1);
        clientBuilder.setForceGlobalBucketAccessEnabled(true);
    }

    s3Client = clientBuilder.build();
    return s3Client;
}

From source file:com.facebook.presto.hive.s3.PrestoS3FileSystem.java

License:Apache License

private AmazonS3Client createAmazonS3Client(URI uri, Configuration hadoopConfig,
        ClientConfiguration clientConfig) {
    AWSCredentialsProvider credentials = getAwsCredentialsProvider(uri, hadoopConfig);
    Optional<EncryptionMaterialsProvider> emp = createEncryptionMaterialsProvider(hadoopConfig);
    AmazonS3Client client;//  ww  w .  ja  v a  2s.  co m
    String signerType = hadoopConfig.get(S3_SIGNER_TYPE);
    if (signerType != null) {
        clientConfig.withSignerOverride(signerType);
    }
    if (emp.isPresent()) {
        client = new AmazonS3EncryptionClient(credentials, emp.get(), clientConfig, new CryptoConfiguration(),
                METRIC_COLLECTOR);
    } else {
        client = new AmazonS3Client(credentials, clientConfig, METRIC_COLLECTOR);
    }

    if (isPathStyleAccess) {
        S3ClientOptions clientOptions = S3ClientOptions.builder().setPathStyleAccess(true).build();
        client.setS3ClientOptions(clientOptions);
    }

    // use local region when running inside of EC2
    if (pinS3ClientToCurrentRegion) {
        Region region = Regions.getCurrentRegion();
        if (region != null) {
            client.setRegion(region);
        }
    }

    String endpoint = hadoopConfig.get(S3_ENDPOINT);
    if (endpoint != null) {
        client.setEndpoint(endpoint);
    }

    return client;
}