1313import org .testcontainers .utility .TestcontainersConfiguration ;
1414
1515import java .net .InetAddress ;
16+ import java .net .URI ;
17+ import java .net .URISyntaxException ;
1618import java .net .UnknownHostException ;
1719import java .util .ArrayList ;
1820import java .util .Arrays ;
@@ -90,7 +92,16 @@ public LocalStackContainer withServices(Service... services) {
9092 .withCredentials(localstack.getDefaultCredentialsProvider())
9193 .build();
9294 </code></pre>
93- *
95+ * or for AWS SDK v2
96+ * <pre><code>S3Client s3 = S3Client
97+ .builder()
98+ .endpointOverride(localstack.getEndpointOverride(LocalStackContainer.Service.S3))
99+ .credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create(
100+ localstack.getAccessKey(), localstack.getSecretKey()
101+ )))
102+ .region(Region.of(localstack.getRegion()))
103+ .build()
104+ </code></pre>
94105 * <p><strong>Please note that this method is only intended to be used for configuring AWS SDK clients
95106 * that are running on the test host. If other containers need to call this one, they should be configured
96107 * specifically to do so using a Docker network and appropriate addressing.</strong></p>
@@ -99,20 +110,41 @@ public LocalStackContainer withServices(Service... services) {
99110 * @return an {@link AwsClientBuilder.EndpointConfiguration}
100111 */
101112 public AwsClientBuilder .EndpointConfiguration getEndpointConfiguration (Service service ) {
102- final String address = getContainerIpAddress ();
103- String ipAddress = address ;
113+ return new AwsClientBuilder .EndpointConfiguration (getEndpointOverride (service ).toString (), getRegion ());
114+ }
115+
116+ /**
117+ * Provides an endpoint override that is preconfigured to communicate with a given simulated service.
118+ * The provided endpoint override should be set in the AWS Java SDK v2 when building a client, e.g.:
119+ * <pre><code>S3Client s3 = S3Client
120+ .builder()
121+ .endpointOverride(localstack.getEndpointOverride(LocalStackContainer.Service.S3))
122+ .credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create(
123+ localstack.getAccessKey(), localstack.getSecretKey()
124+ )))
125+ .region(Region.of(localstack.getRegion()))
126+ .build()
127+ </code></pre>
128+ * <p><strong>Please note that this method is only intended to be used for configuring AWS SDK clients
129+ * that are running on the test host. If other containers need to call this one, they should be configured
130+ * specifically to do so using a Docker network and appropriate addressing.</strong></p>
131+ *
132+ * @param service the service that is to be accessed
133+ * @return an {@link URI} endpoint override
134+ */
135+ public URI getEndpointOverride (Service service ) {
104136 try {
137+ final String address = getContainerIpAddress ();
138+ String ipAddress = address ;
105139 // resolve IP address and use that as the endpoint so that path-style access is automatically used for S3
106140 ipAddress = InetAddress .getByName (address ).getHostAddress ();
107- } catch (UnknownHostException ignored ) {
108-
109- }
110-
111- return new AwsClientBuilder .EndpointConfiguration (
112- "http://" +
141+ return new URI ("http://" +
113142 ipAddress +
114143 ":" +
115- getMappedPort (service .getPort ()), "us-east-1" );
144+ getMappedPort (service .getPort ()));
145+ } catch (UnknownHostException | URISyntaxException e ) {
146+ throw new IllegalStateException ("Cannot obtain endpoint URL" , e );
147+ }
116148 }
117149
118150 /**
@@ -124,10 +156,74 @@ public AwsClientBuilder.EndpointConfiguration getEndpointConfiguration(Service s
124156 .withCredentials(localstack.getDefaultCredentialsProvider())
125157 .build();
126158 </code></pre>
159+ * or for AWS SDK v2 you can use {@link #getAccessKey()}, {@link #getSecretKey()} directly:
160+ * <pre><code>S3Client s3 = S3Client
161+ .builder()
162+ .endpointOverride(localstack.getEndpointOverride(LocalStackContainer.Service.S3))
163+ .credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create(
164+ localstack.getAccessKey(), localstack.getSecretKey()
165+ )))
166+ .region(Region.of(localstack.getRegion()))
167+ .build()
168+ </code></pre>
127169 * @return an {@link AWSCredentialsProvider}
128170 */
129171 public AWSCredentialsProvider getDefaultCredentialsProvider () {
130- return new AWSStaticCredentialsProvider (new BasicAWSCredentials ("accesskey" , "secretkey" ));
172+ return new AWSStaticCredentialsProvider (new BasicAWSCredentials (getAccessKey (), getSecretKey ()));
173+ }
174+
175+ /**
176+ * Provides a default access key that is preconfigured to communicate with a given simulated service.
177+ * The access key can be used to construct AWS SDK v2 clients:
178+ * <pre><code>S3Client s3 = S3Client
179+ .builder()
180+ .endpointOverride(localstack.getEndpointOverride(LocalStackContainer.Service.S3))
181+ .credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create(
182+ localstack.getAccessKey(), localstack.getSecretKey()
183+ )))
184+ .region(Region.of(localstack.getRegion()))
185+ .build()
186+ </code></pre>
187+ * @return a default access key
188+ */
189+ public String getAccessKey () {
190+ return "accesskey" ;
191+ }
192+
193+ /**
194+ * Provides a default secret key that is preconfigured to communicate with a given simulated service.
195+ * The secret key can be used to construct AWS SDK v2 clients:
196+ * <pre><code>S3Client s3 = S3Client
197+ .builder()
198+ .endpointOverride(localstack.getEndpointOverride(LocalStackContainer.Service.S3))
199+ .credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create(
200+ localstack.getAccessKey(), localstack.getSecretKey()
201+ )))
202+ .region(Region.of(localstack.getRegion()))
203+ .build()
204+ </code></pre>
205+ * @return a default secret key
206+ */
207+ public String getSecretKey () {
208+ return "secretkey" ;
209+ }
210+
211+ /**
212+ * Provides a default region that is preconfigured to communicate with a given simulated service.
213+ * The region can be used to construct AWS SDK v2 clients:
214+ * <pre><code>S3Client s3 = S3Client
215+ .builder()
216+ .endpointOverride(localstack.getEndpointOverride(LocalStackContainer.Service.S3))
217+ .credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create(
218+ localstack.getAccessKey(), localstack.getSecretKey()
219+ )))
220+ .region(Region.of(localstack.getRegion()))
221+ .build()
222+ </code></pre>
223+ * @return a default region
224+ */
225+ public String getRegion () {
226+ return "us-east-1" ;
131227 }
132228
133229 @ RequiredArgsConstructor
0 commit comments