Example usage for com.amazonaws.regions AwsRegionProvider getRegion

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

Introduction

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

Prototype

public abstract String getRegion() throws SdkClientException;

Source Link

Usage

From source file:com.netflix.genie.common.internal.aws.s3.S3ClientFactory.java

License:Apache License

/**
 * Constructor./*from ww w . j  av a 2 s  .co  m*/
 *
 * @param awsCredentialsProvider The base AWS credentials provider to use for the generated S3 clients
 * @param regionProvider         How this factory should determine the default {@link Regions}
 * @param environment            The Spring application {@link Environment}
 */
public S3ClientFactory(final AWSCredentialsProvider awsCredentialsProvider,
        final AwsRegionProvider regionProvider, final Environment environment) {
    this.awsCredentialsProvider = awsCredentialsProvider;

    /*
     * Use the Spring property binder to dynamically map properties under a common root into a map of key to object.
     *
     * In this case we're trying to get bucketName -> BucketProperties
     *
     * So if there were properties like:
     * genie.aws.s3.buckets.someBucket1.roleARN = blah
     * genie.aws.s3.buckets.someBucket2.region = us-east-1
     * genie.aws.s3.buckets.someBucket2.roleARN = blah
     *
     * The result of this should be two entries in the map "bucket1" and "bucket2" mapping to property binding
     * object instances of BucketProperties with the correct property set or null if option wasn't specified.
     */
    this.bucketProperties = Binder.get(environment)
            .bind(BUCKET_PROPERTIES_ROOT_KEY, Bindable.mapOf(String.class, BucketProperties.class))
            .orElse(Collections.emptyMap());

    // Set the initial size to the number of special cases defined in properties + 1 for the default client
    // NOTE: Should we proactively create all necessary clients or be lazy about it? For now, lazy.
    final int initialCapacity = this.bucketProperties.size() + 1;
    this.clientCache = new ConcurrentHashMap<>(initialCapacity);
    this.transferManagerCache = new ConcurrentHashMap<>(initialCapacity);

    String tmpRegion;
    try {
        tmpRegion = regionProvider.getRegion();
    } catch (final SdkClientException e) {
        tmpRegion = Regions.getCurrentRegion() != null ? Regions.getCurrentRegion().getName()
                : Regions.US_EAST_1.getName();
        log.warn("Couldn't determine the AWS region from the provider ({}) supplied. Defaulting to {}",
                regionProvider.toString(), tmpRegion);
    }
    this.defaultRegion = Regions.fromName(tmpRegion);

    // Create a token service client to use if we ever need to assume a role
    // TODO: Perhaps this should be just set to null if the bucket properties are empty as we'll never need it?
    this.stsClient = AWSSecurityTokenServiceClientBuilder.standard().withRegion(this.defaultRegion)
            .withCredentials(this.awsCredentialsProvider).build();

    this.bucketToClientKey = new ConcurrentHashMap<>();
}