Skip to content

Commit

Permalink
fix: propagate the configured region to the S3 client (#600)
Browse files Browse the repository at this point in the history
* fix: propagate the configured region to the S3 client and test if the region is actually propagated
  • Loading branch information
sielenk-yara authored Feb 4, 2025
1 parent 91f73fe commit 1d2c9c9
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import java.time.Duration;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.s3.S3AsyncClient;
import software.amazon.awssdk.services.s3.S3CrtAsyncClientBuilder;
import software.amazon.nio.spi.s3.config.S3NioSpiConfiguration;
Expand Down Expand Up @@ -79,6 +80,11 @@ S3CrtAsyncClientBuilder configureCrtClient() {
asyncClientBuilder.credentialsProvider(() -> credentials);
}

var region = configuration.getRegion();
if (region != null) {
asyncClientBuilder.region(Region.of(region));
}

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

Expand Down
22 changes: 22 additions & 0 deletions src/test/java/software/amazon/nio/spi/s3/S3ClientProviderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoExtension;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.s3.S3AsyncClient;
import software.amazon.awssdk.services.s3.S3ServiceClientConfiguration;
import software.amazon.nio.spi.s3.config.S3NioSpiConfiguration;

@ExtendWith(MockitoExtension.class)
Expand Down Expand Up @@ -77,6 +79,26 @@ public void testClosedClientIsNotReused() {
assertNotSame(s3Client, s3Client2);
}

@Test
public void testRegionIsPropagated() {
// expect a non-standard region
final var expectedRegion = Region.AP_SOUTHEAST_2;

// set it in the provider
provider.configuration.withRegion(expectedRegion.id());

try (final var s3Client = provider.generateClient("test-bucket")) {
// get the actual one from the created client
assertNotNull(s3Client);
final var configuration = s3Client.serviceClientConfiguration();
assertNotNull(configuration);
final var actualRegion = configuration.region();

// assert it is as expected
assertSame(expectedRegion, actualRegion);
}
}

@Test
public void generateAsyncClientByEndpointBucketCredentials() {
// GIVEN
Expand Down

0 comments on commit 1d2c9c9

Please sign in to comment.