Skip to content

Commit 06f16fe

Browse files
committed
refactor(S3ClientProvider): extract getBucketLocation and getBucketLocationFromHead methods
1 parent 78fa196 commit 06f16fe

File tree

1 file changed

+21
-13
lines changed

1 file changed

+21
-13
lines changed

src/main/java/software/amazon/nio/spi/s3/S3ClientProvider.java

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -213,21 +213,29 @@ protected <T extends AwsClient> T getClientForBucket(
213213

214214
private String determineBucketLocation(String bucketName, S3Client locationClient) {
215215
try {
216-
logger.debug("determining bucket location with getBucketLocation");
217-
return locationClient.getBucketLocation(builder -> builder.bucket(bucketName)).locationConstraintAsString();
216+
return getBucketLocation(bucketName, locationClient);
218217
} catch (S3Exception e) {
219218
if(isForbidden(e)) {
220-
logger.debug("Cannot determine location of '{}' bucket directly. Attempting to obtain bucket location with headBucket operation", bucketName);
221-
try {
222-
final HeadBucketResponse headBucketResponse = locationClient.headBucket(builder -> builder.bucket(bucketName));
223-
return getBucketRegionFromResponse(headBucketResponse.sdkHttpResponse());
224-
} catch (S3Exception e2) {
225-
if (isRedirect(e2)) {
226-
return getBucketRegionFromResponse(e2.awsErrorDetails().sdkHttpResponse());
227-
} else {
228-
throw e2;
229-
}
230-
}
219+
return getBucketLocationFromHead(bucketName, locationClient);
220+
} else {
221+
throw e;
222+
}
223+
}
224+
}
225+
226+
private String getBucketLocation(String bucketName, S3Client locationClient) {
227+
logger.debug("determining bucket location with getBucketLocation");
228+
return locationClient.getBucketLocation(builder -> builder.bucket(bucketName)).locationConstraintAsString();
229+
}
230+
231+
private String getBucketLocationFromHead(String bucketName, S3Client locationClient) {
232+
try {
233+
logger.debug("Cannot determine location of '{}' bucket directly. Attempting to obtain bucket location with headBucket operation", bucketName);
234+
final HeadBucketResponse headBucketResponse = locationClient.headBucket(builder -> builder.bucket(bucketName));
235+
return getBucketRegionFromResponse(headBucketResponse.sdkHttpResponse());
236+
} catch (S3Exception e) {
237+
if (isRedirect(e)) {
238+
return getBucketRegionFromResponse(e.awsErrorDetails().sdkHttpResponse());
231239
} else {
232240
throw e;
233241
}

0 commit comments

Comments
 (0)