13
13
import org .testcontainers .utility .TestcontainersConfiguration ;
14
14
15
15
import java .net .InetAddress ;
16
+ import java .net .URI ;
17
+ import java .net .URISyntaxException ;
16
18
import java .net .UnknownHostException ;
17
19
import java .util .ArrayList ;
18
20
import java .util .Arrays ;
@@ -90,7 +92,16 @@ public LocalStackContainer withServices(Service... services) {
90
92
.withCredentials(localstack.getDefaultCredentialsProvider())
91
93
.build();
92
94
</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>
94
105
* <p><strong>Please note that this method is only intended to be used for configuring AWS SDK clients
95
106
* that are running on the test host. If other containers need to call this one, they should be configured
96
107
* specifically to do so using a Docker network and appropriate addressing.</strong></p>
@@ -99,20 +110,41 @@ public LocalStackContainer withServices(Service... services) {
99
110
* @return an {@link AwsClientBuilder.EndpointConfiguration}
100
111
*/
101
112
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 ) {
104
136
try {
137
+ final String address = getContainerIpAddress ();
138
+ String ipAddress = address ;
105
139
// resolve IP address and use that as the endpoint so that path-style access is automatically used for S3
106
140
ipAddress = InetAddress .getByName (address ).getHostAddress ();
107
- } catch (UnknownHostException ignored ) {
108
-
109
- }
110
-
111
- return new AwsClientBuilder .EndpointConfiguration (
112
- "http://" +
141
+ return new URI ("http://" +
113
142
ipAddress +
114
143
":" +
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
+ }
116
148
}
117
149
118
150
/**
@@ -124,10 +156,74 @@ public AwsClientBuilder.EndpointConfiguration getEndpointConfiguration(Service s
124
156
.withCredentials(localstack.getDefaultCredentialsProvider())
125
157
.build();
126
158
</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>
127
169
* @return an {@link AWSCredentialsProvider}
128
170
*/
129
171
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" ;
131
227
}
132
228
133
229
@ RequiredArgsConstructor
0 commit comments