-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Im 542 get s3 region by analyzing the asset href #224
Changes from 7 commits
1700b83
038752f
91add51
7d51d03
97eb6aa
8a3e867
3dc711c
e255eda
94b7436
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package org.integratedmodelling.klab.utils.s3; | ||
|
||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
import org.integratedmodelling.klab.exceptions.KlabResourceAccessException; | ||
|
||
import software.amazon.awssdk.auth.credentials.AnonymousCredentialsProvider; | ||
import software.amazon.awssdk.http.apache.ApacheHttpClient; | ||
import software.amazon.awssdk.regions.Region; | ||
import software.amazon.awssdk.services.s3.S3Client; | ||
import software.amazon.awssdk.services.s3.model.GetBucketLocationRequest; | ||
import software.amazon.awssdk.services.s3.model.GetBucketLocationResponse; | ||
import software.amazon.awssdk.services.s3.model.HeadObjectRequest; | ||
|
||
public class S3RegionResolver { | ||
|
||
public static Region resolveBucketRegion(String bucketName, String objectKey) { | ||
// Default region to start with for GetBucketLocation | ||
Region defaultRegion = Region.US_EAST_1; | ||
|
||
// Step 1: Attempt to dynamically resolve the bucket's region | ||
try (S3Client s3 = S3Client.builder() | ||
.httpClientBuilder(ApacheHttpClient.builder()) | ||
.region(defaultRegion) // Use the default region | ||
.credentialsProvider(AnonymousCredentialsProvider.create()) // Anonymous credentials | ||
.build()) { | ||
|
||
GetBucketLocationRequest request = GetBucketLocationRequest.builder() | ||
.bucket(bucketName) | ||
.build(); | ||
|
||
GetBucketLocationResponse response = s3.getBucketLocation(request); | ||
String location = response.locationConstraintAsString(); | ||
|
||
// Handle "null" or "global" regions | ||
if (location == null || location.equalsIgnoreCase("null")) { | ||
return Region.US_EAST_1; | ||
} | ||
|
||
Region resolvedRegion = Region.of(location); | ||
return resolvedRegion; | ||
|
||
} catch (Exception e) { | ||
// try another one | ||
} | ||
|
||
// Step 2: Iterate through all regions and test lightweight requests | ||
return resolveRegionByTesting(bucketName, objectKey); | ||
} | ||
|
||
private static List<Region> getAwsRegions() { | ||
// List of regions to exclude (e.g., isolated regions or restricted access regions) | ||
List<Region> excludedRegions = List.of( | ||
Region.US_ISO_EAST_1, // Restricted to isolated networks | ||
Region.US_ISO_WEST_1, // Example of another restricted region | ||
Region.CN_NORTH_1, // China regions may require special accounts | ||
Region.CN_NORTHWEST_1 | ||
); | ||
|
||
// Get the list of all AWS regions, excluding problematic ones | ||
return Region.regions().stream() | ||
.filter(region -> !excludedRegions.contains(region)) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
private static Region resolveRegionByTesting(String bucketName, String objectKey) { | ||
// Get the list of all AWS regions | ||
List<Region> regions = getAwsRegions(); | ||
|
||
// Iterate through regions to perform a lightweight test | ||
for (Region region : regions) { | ||
try (S3Client s3 = S3Client.builder() | ||
.region(region) | ||
.credentialsProvider(AnonymousCredentialsProvider.create()) | ||
.build()) { | ||
|
||
System.out.println("Testing region: " + region); | ||
|
||
// Perform a lightweight HEAD request to check if the object exists | ||
HeadObjectRequest request = HeadObjectRequest.builder() | ||
.bucket(bucketName) | ||
.key(objectKey) | ||
.build(); | ||
|
||
s3.headObject(request); // If no exception is thrown, the region is correct | ||
System.out.println("Region confirmed by testing: " + region); | ||
return region; | ||
} catch (Exception e) { | ||
// Continue testing other regions if the bucket is not found | ||
continue; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This catch is very general, could it be interesting to write a warning to the log file? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am not sure about adding the logging class here, as the loop will fail most times. I could manage the exceptions in a more specialized way. |
||
} | ||
} | ||
|
||
throw new KlabResourceAccessException("Unable to resolve region for bucket: " + bucketName); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package org.integratedmodelling.klab.test.utils; | ||
|
||
import org.integratedmodelling.klab.exceptions.KlabResourceAccessException; | ||
import org.integratedmodelling.klab.utils.s3.S3RegionResolver; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import software.amazon.awssdk.regions.Region; | ||
|
||
public class S3RegionResolverTest { | ||
|
||
@Test | ||
public void resolveBucketRegion_resolutionSuccessful() { | ||
String bucket = "deafrica-input-datasets"; | ||
String objectKey = "rainfall_chirps_monthly/chirps-v2.0_2024.03.tif"; | ||
|
||
Region ret = S3RegionResolver.resolveBucketRegion(bucket, objectKey); | ||
|
||
Assertions.assertEquals(Region.AF_SOUTH_1, ret); | ||
} | ||
|
||
@Test | ||
// Warning: a relatively costly test (sometimes over 30 seconds) | ||
public void resolveBucketRegion_resolutionUnsuccessful() { | ||
String bucket = "fake-bucket"; | ||
String objectKey = "fake-object.tif"; | ||
|
||
Assertions.assertThrows(KlabResourceAccessException.class, () -> { | ||
S3RegionResolver.resolveBucketRegion(bucket, objectKey); | ||
}); | ||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This catch is very general, could it be interesting to write a warning to the log file?