Skip to content

Commit

Permalink
Merge pull request #34 from saalfeldlab/s3ClientConfig
Browse files Browse the repository at this point in the history
feat: AmazonS3Utils add ClientConfiguration
  • Loading branch information
bogovicj authored Jun 5, 2024
2 parents f2901bd + 3a9c546 commit 8c1024f
Showing 1 changed file with 31 additions and 3 deletions.
34 changes: 31 additions & 3 deletions src/main/java/org/janelia/saalfeldlab/n5/s3/AmazonS3Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import org.janelia.saalfeldlab.n5.N5Exception;

import com.amazonaws.ClientConfiguration;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.AWSCredentialsProvider;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
Expand Down Expand Up @@ -117,7 +118,14 @@ public static AmazonS3 createS3(final String uri) {
return createS3(uri, (String)null, null, null);
}

public static AmazonS3 createS3(final String uri, @Nullable final String s3Endpoint, @Nullable final AWSCredentialsProvider s3Credentials, @Nullable String region) {
public static AmazonS3 createS3(final String uri, @Nullable final String s3Endpoint, @Nullable final AWSCredentialsProvider s3Credentials,
@Nullable String region) {

return createS3(uri, s3Endpoint, s3Credentials, null, region);
}

public static AmazonS3 createS3(final String uri, @Nullable final String s3Endpoint, @Nullable final AWSCredentialsProvider s3Credentials,
@Nullable ClientConfiguration clientConfiguration, @Nullable String region) {

try {
final AmazonS3URI s3Uri = new AmazonS3URI(uri);
Expand All @@ -134,13 +142,20 @@ public static AmazonS3 createS3(final String uri, @Nullable final String s3Endpo
}
}

public static AmazonS3 createS3(final AmazonS3URI s3Uri, @Nullable final String s3Endpoint, @Nullable final AWSCredentialsProvider s3Credentials, @Nullable final String region) {
public static AmazonS3 createS3(final AmazonS3URI s3Uri, @Nullable final String s3Endpoint, @Nullable final AWSCredentialsProvider s3Credentials,
@Nullable final String region) {

return createS3(s3Uri, s3Endpoint, s3Credentials, null, region);
}

public static AmazonS3 createS3(final AmazonS3URI s3Uri, @Nullable final String s3Endpoint, @Nullable final AWSCredentialsProvider s3Credentials,
@Nullable ClientConfiguration clientConfiguration, @Nullable final String region) {

AwsClientBuilder.EndpointConfiguration endpointConfiguration = null;
if (!S3_SCHEME.matcher(s3Uri.getURI().getScheme()).matches()) {
endpointConfiguration = createEndpointConfiguration(s3Uri, s3Endpoint);
}
return createS3(s3Uri.getBucket(), s3Credentials, endpointConfiguration, getS3Region(s3Uri, region));
return createS3(s3Uri.getBucket(), s3Credentials, endpointConfiguration, clientConfiguration, getS3Region(s3Uri, region));
}

public static AwsClientBuilder.EndpointConfiguration createEndpointConfiguration(final AmazonS3URI s3Uri, @Nullable final String s3Endpoint) {
Expand All @@ -164,6 +179,16 @@ public static AmazonS3 createS3(
@Nullable final AwsClientBuilder.EndpointConfiguration endpointConfiguration,
@Nullable final Regions region) {

return createS3(bucketName, credentialsProvider, endpointConfiguration, null, region);
}

public static AmazonS3 createS3(
final String bucketName,
@Nullable final AWSCredentialsProvider credentialsProvider,
@Nullable final AwsClientBuilder.EndpointConfiguration endpointConfiguration,
@Nullable final ClientConfiguration clientConfiguration,
@Nullable final Regions region) {

final boolean isAmazon = endpointConfiguration == null || AmazonS3Utils.AWS_ENDPOINT_PATTERN.matcher(endpointConfiguration.getServiceEndpoint()).find();
final AmazonS3ClientBuilder builder = AmazonS3ClientBuilder.standard();

Expand All @@ -175,6 +200,9 @@ public static AmazonS3 createS3(
else
builder.withCredentials(credentialsProvider);

if (clientConfiguration != null)
builder.withClientConfiguration(clientConfiguration);

if (endpointConfiguration != null)
builder.withEndpointConfiguration(endpointConfiguration);
else if (region != null)
Expand Down

0 comments on commit 8c1024f

Please sign in to comment.