Skip to content

Commit 5b0a788

Browse files
committed
Fix the bug of client endpoint override being ignored
1 parent 5a1a787 commit 5b0a788

File tree

3 files changed

+40
-2
lines changed

3 files changed

+40
-2
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"category": "AWS S3",
3+
"contributor": "",
4+
"type": "bugfix",
5+
"description": "Fix the bug that getUrl in S3Utilities ignores the overriding endpoint in the client."
6+
}

services/s3/src/main/java/software/amazon/awssdk/services/s3/S3Utilities.java

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
@SdkPublicApi
7474
public final class S3Utilities {
7575
private final Region region;
76+
private final URI endpoint;
7677
private final S3Configuration s3Configuration;
7778
private final ProfileFile profileFile;
7879
private final String profileName;
@@ -83,6 +84,7 @@ public final class S3Utilities {
8384
*/
8485
private S3Utilities(Builder builder) {
8586
this.region = Validate.paramNotNull(builder.region, "Region");
87+
this.endpoint = builder.endpoint;
8688
this.s3Configuration = builder.s3Configuration;
8789
this.profileFile = builder.profileFile;
8890
this.profileName = builder.profileName;
@@ -100,6 +102,7 @@ public static Builder builder() {
100102
static S3Utilities create(SdkClientConfiguration clientConfiguration) {
101103
return S3Utilities.builder()
102104
.region(clientConfiguration.option(AwsClientOption.AWS_REGION))
105+
.endpoint(clientConfiguration.option(SdkClientOption.ENDPOINT))
103106
.s3Configuration((S3Configuration) clientConfiguration.option(SdkClientOption.SERVICE_CONFIGURATION))
104107
.profileFile(clientConfiguration.option(SdkClientOption.PROFILE_FILE))
105108
.profileName(clientConfiguration.option(SdkClientOption.PROFILE_NAME))
@@ -188,8 +191,9 @@ private Region resolveRegionForGetUrl(GetUrlRequest getUrlRequest) {
188191
/**
189192
* If endpoint is not present, construct a default endpoint using the region information.
190193
*/
191-
private URI resolveEndpoint(URI endpoint, Region region) {
192-
return endpoint != null ? endpoint
194+
private URI resolveEndpoint(URI requestOverrideEndpoint, Region region) {
195+
URI overrideEndpoint = requestOverrideEndpoint != null ? requestOverrideEndpoint : endpoint;
196+
return overrideEndpoint != null ? overrideEndpoint
193197
: new DefaultServiceEndpointBuilder("s3", "https").withRegion(region)
194198
.withProfileFile(profileFile)
195199
.withProfileName(profileName)
@@ -227,6 +231,7 @@ private SdkHttpFullRequest createMarshalledRequest(GetUrlRequest getUrlRequest,
227231
*/
228232
public static final class Builder {
229233
private Region region;
234+
private URI endpoint;
230235

231236
private S3Configuration s3Configuration;
232237
private ProfileFile profileFile;
@@ -248,6 +253,19 @@ public Builder region(Region region) {
248253
return this;
249254
}
250255

256+
/**
257+
* The default endpoint to use when working with the methods in {@link S3Utilities} class.
258+
*
259+
* There can be methods in {@link S3Utilities} that don't need the endpoint info.
260+
* In that case, this option will be ignored when using those methods.
261+
*
262+
* @return This object for method chaining
263+
*/
264+
public Builder endpoint(URI endpoint) {
265+
this.endpoint = endpoint;
266+
return this;
267+
}
268+
251269
/**
252270
* Sets the S3 configuration to enable options like path style access, dual stack, accelerate mode etc.
253271
*

services/s3/src/test/java/software/amazon/awssdk/services/s3/S3UtilitiesTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,20 @@ public void test_EndpointOnRequestTakesPrecendence() throws MalformedURLExceptio
130130
.isEqualTo("https://foo-bucket.s3.amazonaws.com/key-without-spaces");
131131
}
132132

133+
@Test
134+
public void test_EndpointOverrideOnClientWorks() {
135+
S3Utilities customizeUtilities = S3Client.builder()
136+
.endpointOverride(URI.create("https://s3.custom.host"))
137+
.build()
138+
.utilities();
139+
assertThat(customizeUtilities.getUrl(GetUrlRequest.builder()
140+
.bucket("foo-bucket")
141+
.key("key-without-spaces")
142+
.build())
143+
.toExternalForm())
144+
.isEqualTo("https://foo-bucket.s3.custom.host/key-without-spaces");
145+
}
146+
133147
@Test
134148
public void testWithAccelerateAndDualStackEnabled() throws MalformedURLException {
135149
S3Utilities utilities = S3Client.builder()

0 commit comments

Comments
 (0)