All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.nosqlbench.nb.addins.s3.s3urlhandler.S3ClientCache Maven / Gradle / Ivy

Go to download

The top level API module for NoSQLBench. This module should have no internal module dependencies other than the mvn-default module. All modules within NoSQLBench can safely depend on this module with circular dependencies. This module provides cross-cutting code infrastracture, such as path utilities and ways of describing services used between modules. It is also the transitive aggregation point for system-wide library dependencies for logging and testing or similar needs.

There is a newer version: 5.17.0
Show newest version
package io.nosqlbench.nb.addins.s3.s3urlhandler;

import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;

import java.util.WeakHashMap;

/**
 * This client cache uses the credentials provided in a URL to create
 * a fingerprint, and then creates a customized S3 client for each unique
 * instance. If these clients are not used, they are allowed to be expired
 * from the map and collected.
 */
public class S3ClientCache {

    private final WeakHashMap cache = new WeakHashMap<>();

    public S3ClientCache() {
    }

    public AmazonS3 get(S3UrlFields fields) {
        AmazonS3 s3 = cache.computeIfAbsent(fields.getCredentialsFingerprint(),
            cfp -> createAuthorizedClient(fields));
        return s3;
    }

    private AmazonS3 createAuthorizedClient(S3UrlFields fields) {
        if (fields.accessKey!=null && fields.secretKey!=null) {
            AmazonS3ClientBuilder builder = AmazonS3ClientBuilder.standard();
            AWSCredentials specialcreds = new BasicAWSCredentials(fields.accessKey, fields.secretKey);
            builder = builder.withCredentials(new AWSStaticCredentialsProvider(specialcreds));
            return builder.build();
        } else {
            return AmazonS3ClientBuilder.defaultClient();
        }
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy