Search in sources :

Example 1 with NexusS3ClientBuilder

use of com.amazonaws.services.s3.NexusS3ClientBuilder in project nexus-public by sonatype.

the class AmazonS3Factory method create.

public AmazonS3 create(final BlobStoreConfiguration blobStoreConfiguration) {
    NexusS3ClientBuilder builder = NexusS3ClientBuilder.standard();
    NestedAttributesMap s3Configuration = blobStoreConfiguration.attributes(CONFIG_KEY);
    String accessKeyId = s3Configuration.get(ACCESS_KEY_ID_KEY, String.class);
    String secretAccessKey = s3Configuration.get(SECRET_ACCESS_KEY_KEY, String.class);
    String region = s3Configuration.get(REGION_KEY, String.class);
    String signerType = s3Configuration.get(SIGNERTYPE_KEY, String.class);
    String forcePathStyle = s3Configuration.get(FORCE_PATH_STYLE_KEY, String.class);
    int maximumConnectionPoolSize = Optional.ofNullable(s3Configuration.get(MAX_CONNECTION_POOL_KEY, String.class)).filter(Predicates.not(Strings2::isBlank)).map(Integer::valueOf).orElse(-1);
    AWSCredentialsProvider credentialsProvider = null;
    if (!isNullOrEmpty(accessKeyId) && !isNullOrEmpty(secretAccessKey)) {
        String sessionToken = s3Configuration.get(SESSION_TOKEN_KEY, String.class);
        AWSCredentials credentials = buildCredentials(accessKeyId, secretAccessKey, sessionToken);
        String assumeRole = s3Configuration.get(ASSUME_ROLE_KEY, String.class);
        credentialsProvider = buildCredentialsProvider(credentials, region, assumeRole);
        builder = builder.withCredentials(credentialsProvider);
    }
    if (!isNullOrEmptyOrDefault(region)) {
        String endpoint = s3Configuration.get(ENDPOINT_KEY, String.class);
        if (!isNullOrEmpty(endpoint)) {
            builder = builder.withEndpointConfiguration(new AmazonS3ClientBuilder.EndpointConfiguration(endpoint, region));
        } else {
            builder = builder.withRegion(region);
        }
    }
    ClientConfiguration clientConfiguration = PredefinedClientConfigurations.defaultConfig();
    if (defaultConnectionPoolSize > 0 || maximumConnectionPoolSize > 0) {
        clientConfiguration.setMaxConnections(maximumConnectionPoolSize > 0 ? maximumConnectionPoolSize : defaultConnectionPoolSize);
    }
    if (!isNullOrEmptyOrDefault(signerType)) {
        clientConfiguration.setSignerOverride(signerType);
    }
    if (connectionTtl != null) {
        clientConfiguration.setConnectionTTL(connectionTtl.toMillis());
    }
    builder = builder.withClientConfiguration(clientConfiguration);
    builder = builder.withPathStyleAccessEnabled(Boolean.parseBoolean(forcePathStyle));
    builder.withBlobStoreConfig(blobStoreConfiguration);
    if (cloudWatchMetricsEnabled) {
        if (credentialsProvider != null) {
            AwsSdkMetrics.setCredentialProvider(credentialsProvider);
        }
        AwsSdkMetrics.setMetricNameSpace(cloudWatchMetricsNamespace);
        if (!isNullOrEmptyOrDefault(region)) {
            AwsSdkMetrics.setRegion(region);
        }
        AwsSdkMetrics.enableDefaultMetrics();
        log.info("CloudWatch metrics enabled using namespace {}", cloudWatchMetricsNamespace);
    }
    return builder.build();
}
Also used : NexusS3ClientBuilder(com.amazonaws.services.s3.NexusS3ClientBuilder) AWSCredentials(com.amazonaws.auth.AWSCredentials) BasicAWSCredentials(com.amazonaws.auth.BasicAWSCredentials) NestedAttributesMap(org.sonatype.nexus.common.collect.NestedAttributesMap) AWSCredentialsProvider(com.amazonaws.auth.AWSCredentialsProvider) ClientConfiguration(com.amazonaws.ClientConfiguration)

Aggregations

ClientConfiguration (com.amazonaws.ClientConfiguration)1 AWSCredentials (com.amazonaws.auth.AWSCredentials)1 AWSCredentialsProvider (com.amazonaws.auth.AWSCredentialsProvider)1 BasicAWSCredentials (com.amazonaws.auth.BasicAWSCredentials)1 NexusS3ClientBuilder (com.amazonaws.services.s3.NexusS3ClientBuilder)1 NestedAttributesMap (org.sonatype.nexus.common.collect.NestedAttributesMap)1