Skip to content

Commit

Permalink
chore: extract configureCrtClientForRegion method
Browse files Browse the repository at this point in the history
  • Loading branch information
guicamest committed Nov 4, 2023
1 parent 7677368 commit abd07fd
Showing 1 changed file with 27 additions and 25 deletions.
52 changes: 27 additions & 25 deletions src/main/java/software/amazon/nio/spi/s3/S3ClientProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,15 @@ public class S3ClientProvider {
final protected S3NioSpiConfiguration configuration;

/**
* Default client using the "https://s3.us-east-1.amazonaws.com" endpoint
* Default client using the "<a href="https://s3.us-east-1.amazonaws.com">...</a>" endpoint
*/
private static final S3Client DEFAULT_CLIENT = S3Client.builder()
.endpointOverride(URI.create("https://s3.us-east-1.amazonaws.com"))
.region(Region.US_EAST_1)
.build();

/**
* Default asynchronous client using the "https://s3.us-east-1.amazonaws.com" endpoint
* Default asynchronous client using the "<a href="https://s3.us-east-1.amazonaws.com">...</a>" endpoint
*/
private static final S3AsyncClient DEFAULT_ASYNC_CLIENT = S3AsyncClient.builder()
.endpointOverride(URI.create("https://s3.us-east-1.amazonaws.com"))
Expand Down Expand Up @@ -184,11 +184,8 @@ private <T extends AwsClient> T getClientForBucket(
logger.debug("generating client for bucket: '{}'", bucketName);
T bucketSpecificClient = null;

if ((configuration.getEndpoint() == null) || configuration.getEndpoint().isBlank()) {
//
// we try to locate a bucket only if no endpoint is provided, which
// means we are dealing with AWS S3 buckets
//
if (configuration.endpointURI() == null) {
// we try to locate a bucket only if no endpoint is provided, which means we are dealing with AWS S3 buckets
String bucketLocation = determineBucketLocation(bucketName, locationClient);

if ( bucketLocation != null) {
Expand Down Expand Up @@ -253,24 +250,7 @@ private S3AsyncClient asyncClientForRegion(String regionName, boolean crt) {
if (!crt) {
return configureClientForRegion(regionName, S3AsyncClient.builder());
}
Region region = getRegionFromRegionName(regionName);
logger.debug("bucket region is: '{}'", region.id());

URI endpointUri = configuration.endpointURI();
if (endpointUri != null) {
asyncClientBuilder.endpointOverride(endpointUri);
}

AwsCredentials credentials = configuration.getCredentials();
if (credentials != null) {
asyncClientBuilder.credentialsProvider(() -> credentials);
}

return asyncClientBuilder.forcePathStyle(configuration.getForcePathStyle()).region(region).build();
}

private static Region getRegionFromRegionName(String regionName) {
return (regionName == null || regionName.isBlank()) ? Region.US_EAST_1 : Region.of(regionName);
return configureCrtClientForRegion(regionName);
}

private <ActualClient extends AwsClient, ActualBuilder extends S3BaseClientBuilder<ActualBuilder, ActualClient>> ActualClient configureClientForRegion(
Expand Down Expand Up @@ -301,4 +281,26 @@ private <ActualClient extends AwsClient, ActualBuilder extends S3BaseClientBuild

return builder.build();
}

private S3AsyncClient configureCrtClientForRegion(String regionName) {
Region region = getRegionFromRegionName(regionName);
logger.debug("bucket region is: '{}'", region.id());

URI endpointUri = configuration.endpointURI();
if (endpointUri != null) {
asyncClientBuilder.endpointOverride(endpointUri);
}

AwsCredentials credentials = configuration.getCredentials();
if (credentials != null) {
asyncClientBuilder.credentialsProvider(() -> credentials);
}

return asyncClientBuilder.forcePathStyle(configuration.getForcePathStyle()).region(region).build();
}

private static Region getRegionFromRegionName(String regionName) {
return (regionName == null || regionName.isBlank()) ? Region.US_EAST_1 : Region.of(regionName);
}

}

0 comments on commit abd07fd

Please sign in to comment.