diff --git a/modules/localstack/src/main/java/org/testcontainers/containers/localstack/LocalStackContainer.java b/modules/localstack/src/main/java/org/testcontainers/containers/localstack/LocalStackContainer.java index 38bbeba6130..9c7d7298223 100644 --- a/modules/localstack/src/main/java/org/testcontainers/containers/localstack/LocalStackContainer.java +++ b/modules/localstack/src/main/java/org/testcontainers/containers/localstack/LocalStackContainer.java @@ -254,6 +254,35 @@ public URI getEndpointOverride(EnabledService service) { } } + /** + * Provides an endpoint to communicate with LocalStack service. + * The provided endpoint should be set in the AWS Java SDK v2 when building a client, e.g.: + *
S3Client s3 = S3Client
+ .builder()
+ .endpointOverride(localstack.getEndpoint())
+ .credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create(
+ localstack.getAccessKey(), localstack.getSecretKey()
+ )))
+ .region(Region.of(localstack.getRegion()))
+ .build()
+
+ * Please note that this method is only intended to be used for configuring AWS SDK clients + * that are running on the test host. If other containers need to call this one, they should be configured + * specifically to do so using a Docker network and appropriate addressing.
+ * + * @return an {@link URI} endpoint + */ + public URI getEndpoint() { + try { + final String address = getHost(); + // resolve IP address and use that as the endpoint so that path-style access is automatically used for S3 + String ipAddress = InetAddress.getByName(address).getHostAddress(); + return new URI("http://" + ipAddress + ":" + getMappedPort(PORT)); + } catch (UnknownHostException | URISyntaxException e) { + throw new IllegalStateException("Cannot obtain endpoint URL", e); + } + } + private int getServicePort(EnabledService service) { return legacyMode ? service.getPort() : PORT; } diff --git a/modules/localstack/src/test/java/org/testcontainers/containers/localstack/LocalstackContainerTest.java b/modules/localstack/src/test/java/org/testcontainers/containers/localstack/LocalstackContainerTest.java index 30682388af5..f9f1e1c372a 100644 --- a/modules/localstack/src/test/java/org/testcontainers/containers/localstack/LocalstackContainerTest.java +++ b/modules/localstack/src/test/java/org/testcontainers/containers/localstack/LocalstackContainerTest.java @@ -76,7 +76,7 @@ public void s3TestOverBridgeNetwork() throws IOException { .standard() .withEndpointConfiguration( new AwsClientBuilder.EndpointConfiguration( - localstack.getEndpointOverride(Service.S3).toString(), + localstack.getEndpoint().toString(), localstack.getRegion() ) ) @@ -115,7 +115,7 @@ public void s3TestUsingAwsSdkV2() { // with_aws_sdk_v2 { S3Client s3 = S3Client .builder() - .endpointOverride(localstack.getEndpointOverride(Service.S3)) + .endpointOverride(localstack.getEndpoint()) .credentialsProvider( StaticCredentialsProvider.create( AwsBasicCredentials.create(localstack.getAccessKey(), localstack.getSecretKey()) @@ -138,7 +138,7 @@ public void sqsTestOverBridgeNetwork() { .standard() .withEndpointConfiguration( new AwsClientBuilder.EndpointConfiguration( - localstack.getEndpointOverride(Service.SQS).toString(), + localstack.getEndpoint().toString(), localstack.getRegion() ) ) @@ -171,7 +171,7 @@ public void cloudWatchLogsTestOverBridgeNetwork() { .standard() .withEndpointConfiguration( new AwsClientBuilder.EndpointConfiguration( - localstack.getEndpointOverride(Service.CLOUDWATCHLOGS).toString(), + localstack.getEndpoint().toString(), localstack.getRegion() ) ) @@ -195,7 +195,7 @@ public void kmsKeyCreationTest() { .standard() .withEndpointConfiguration( new AwsClientBuilder.EndpointConfiguration( - localstack.getEndpointOverride(Service.KMS).toString(), + localstack.getEndpoint().toString(), localstack.getRegion() ) ) @@ -345,7 +345,7 @@ public static class WithRegion { @Test public void s3EndpointHasProperRegion() { final AwsClientBuilder.EndpointConfiguration endpointConfiguration = new AwsClientBuilder.EndpointConfiguration( - localstack.getEndpointOverride(Service.S3).toString(), + localstack.getEndpoint().toString(), localstack.getRegion() ); assertThat(endpointConfiguration.getSigningRegion()) @@ -366,7 +366,7 @@ public void s3ServiceStartLazily() { try ( S3Client s3 = S3Client .builder() - .endpointOverride(localstack.getEndpointOverride(Service.S3)) + .endpointOverride(localstack.getEndpoint()) .credentialsProvider( StaticCredentialsProvider.create( AwsBasicCredentials.create(localstack.getAccessKey(), localstack.getSecretKey())