Example usage for com.amazonaws.services.s3 AmazonS3URI getRegion

List of usage examples for com.amazonaws.services.s3 AmazonS3URI getRegion

Introduction

In this page you can find the example usage for com.amazonaws.services.s3 AmazonS3URI getRegion.

Prototype

public String getRegion() 

Source Link

Usage

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

License:Apache License

/**
 * Get an {@link AmazonS3} client instance appropriate for the given {@link AmazonS3URI}.
 *
 * @param s3URI The URI of the S3 resource this client is expected to access.
 * @return A S3 client instance which should be used to access the S3 resource
 *///w w  w  .  jav a 2  s.c o m
public AmazonS3 getClient(final AmazonS3URI s3URI) {
    final String bucketName = s3URI.getBucket();

    final S3ClientKey s3ClientKey;

    /*
     * The purpose of the dual maps is to make sure we don't create an unnecessary number of S3 clients.
     * If we made the client cache just bucketName -> client directly we'd have no way to make know if an already
     * created instance for another bucket could be re-used for this bucket since it could be same region/role
     * combination. This way we first map the bucket name to a key of role/region and then use that key
     * to find a re-usable client for those dimensions.
     */
    s3ClientKey = this.bucketToClientKey.computeIfAbsent(bucketName, key -> {
        // We've never seen this bucket before. Calculate the key.

        /*
         * Region Resolution rules:
         * 1. Is it part of the S3 URI already? Use that
         * 2. Is it part of the properties passed in by admin/user Use that
         * 3. Fall back to whatever the default is for this process
         */
        final Regions bucketRegion;
        final String uriBucketRegion = s3URI.getRegion();
        if (StringUtils.isNotBlank(uriBucketRegion)) {
            bucketRegion = Regions.fromName(uriBucketRegion);
        } else {
            final String propertyBucketRegion = this.bucketProperties.containsKey(key)
                    ? this.bucketProperties.get(key).getRegion().orElse(null)
                    : null;

            if (StringUtils.isNotBlank(propertyBucketRegion)) {
                bucketRegion = Regions.fromName(propertyBucketRegion);
            } else {
                bucketRegion = this.defaultRegion;
            }
        }

        // Anything special in the bucket we need to reference
        final String roleARN = this.bucketProperties.containsKey(key)
                ? this.bucketProperties.get(key).getRoleARN().orElse(null)
                : null;

        return new S3ClientKey(bucketRegion, roleARN);
    });

    return this.clientCache.computeIfAbsent(s3ClientKey, this::buildS3Client);
}