-
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
Merged
inigo-cobian
merged 9 commits into
develop
from
IM-542-Get-S3-region-by-analyzing-the-asset-href
Feb 3, 2025
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
1700b83
Resolve AWS region at STAC importer
inigo-cobian 038752f
Remove parameter s3BucketRegion
inigo-cobian 91add51
Do not use item_assets to obtain asset data
inigo-cobian 7d51d03
Set HTTP provicer for S3Client
inigo-cobian 97eb6aa
Test S3RegionResolver
inigo-cobian 8a3e867
Refactor S3RegionResolver
inigo-cobian 3dc711c
Update STACEncoder to fit with the new RegionResolver
inigo-cobian e255eda
Set monitor to S3RegionResolver
inigo-cobian 94b7436
Update S3RegionResolverTest
inigo-cobian File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
klab.engine/src/main/java/org/integratedmodelling/klab/utils/s3/S3RegionResolver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package org.integratedmodelling.klab.utils.s3; | ||
|
||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
import org.integratedmodelling.klab.api.runtime.monitoring.IMonitor; | ||
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; | ||
import software.amazon.awssdk.services.s3.model.S3Exception; | ||
|
||
public class S3RegionResolver { | ||
|
||
public static Region resolveBucketRegion(String bucketName, String objectKey, IMonitor monitor) { | ||
// Step 1: Attempt to dynamically resolve the bucket's region | ||
try (S3Client s3 = S3Client.builder() | ||
.httpClientBuilder(ApacheHttpClient.builder()) | ||
.region(Region.US_EAST_1) // 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); | ||
monitor.debug("Bucket " + bucketName + " exists in the default region."); | ||
return resolvedRegion; | ||
} catch (S3Exception e) { | ||
;// Nothing to do here. It is expected to fail if the bucket is not in the default region. Try on step 2. | ||
} catch (Exception e) { | ||
monitor.debug("Unexpected exception trying to get to the S3 default region: " + e.getMessage()); | ||
} | ||
|
||
// Step 2: Iterate through all regions and test lightweight requests | ||
return resolveRegionByTesting(bucketName, objectKey, monitor); | ||
} | ||
|
||
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, IMonitor monitor) { | ||
// 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() | ||
.httpClientBuilder(ApacheHttpClient.builder()) | ||
.region(region) | ||
.credentialsProvider(AnonymousCredentialsProvider.create()) | ||
.build()) { | ||
|
||
// 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 | ||
monitor.debug("Bucket " + bucketName + " exists in the region " + region.id() + "."); | ||
return region; | ||
} catch (S3Exception e) { | ||
// Continue testing other regions if the bucket is not found | ||
continue; | ||
} catch (Exception e) { | ||
monitor.debug("Unexpected exception trying to get to the S3 region " + region.id() + ": " + e.getMessage()); | ||
continue; | ||
} | ||
} | ||
|
||
throw new KlabResourceAccessException("Unable to resolve region for bucket: " + bucketName); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
klab.engine/src/test/java/org/integratedmodelling/klab/test/utils/S3RegionResolverTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package org.integratedmodelling.klab.test.utils; | ||
|
||
import org.integratedmodelling.klab.api.runtime.monitoring.IMonitor; | ||
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 org.mockito.Mockito; | ||
|
||
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"; | ||
IMonitor monitor = Mockito.mock(IMonitor.class); | ||
|
||
Region ret = S3RegionResolver.resolveBucketRegion(bucket, objectKey, monitor); | ||
|
||
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"; | ||
IMonitor monitor = Mockito.mock(IMonitor.class); | ||
|
||
Assertions.assertThrows(KlabResourceAccessException.class, () -> { | ||
S3RegionResolver.resolveBucketRegion(bucket, objectKey, monitor); | ||
}); | ||
} | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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?
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.
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.