-
Notifications
You must be signed in to change notification settings - Fork 325
Add top-level filters option for S3 source #6735
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
Merged
dlvenable
merged 2 commits into
opensearch-project:main
from
lawofcycles:feature/s3-source-top-level-filters
Apr 21, 2026
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
57 changes: 57 additions & 0 deletions
57
...-source/src/main/java/org/opensearch/dataprepper/plugins/source/s3/S3ObjectKeyFilter.java
This file contains hidden or 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,57 @@ | ||
| /* | ||
| * Copyright OpenSearch Contributors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| * | ||
| * The OpenSearch Contributors require contributions made to | ||
| * this file be licensed under the Apache-2.0 license or a | ||
| * compatible open source license. | ||
| * | ||
| */ | ||
|
|
||
| package org.opensearch.dataprepper.plugins.source.s3; | ||
|
|
||
| import org.opensearch.dataprepper.plugins.source.s3.configuration.S3ScanKeyPathOption; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| /** | ||
| * Filters S3 objects by key using top-level include_prefix and exclude_suffix options. | ||
| */ | ||
| public class S3ObjectKeyFilter { | ||
|
|
||
| private final Map<String, S3ScanKeyPathOption> filters; | ||
|
|
||
| public S3ObjectKeyFilter(final Map<String, S3ScanKeyPathOption> filters) { | ||
| this.filters = filters; | ||
| } | ||
|
|
||
| /** | ||
| * Returns true if the object key matches the filters for the given bucket, | ||
| * or if no filters are configured for the bucket. | ||
| */ | ||
| public boolean isKeyMatchingFilters(final String bucketName, final String objectKey) { | ||
| if (filters == null || filters.isEmpty()) { | ||
| return true; | ||
| } | ||
|
|
||
| final S3ScanKeyPathOption keyPathOption = filters.get(bucketName); | ||
| if (keyPathOption == null) { | ||
| return true; | ||
| } | ||
|
|
||
| final List<String> includePrefixes = keyPathOption.getS3scanIncludePrefixOptions(); | ||
| if (includePrefixes != null && !includePrefixes.isEmpty() | ||
| && includePrefixes.stream().noneMatch(objectKey::startsWith)) { | ||
| return false; | ||
| } | ||
|
|
||
| final List<String> excludeSuffixes = keyPathOption.getS3ScanExcludeSuffixOptions(); | ||
| if (excludeSuffixes != null && !excludeSuffixes.isEmpty() | ||
| && excludeSuffixes.stream().anyMatch(objectKey::endsWith)) { | ||
| return false; | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -22,8 +22,10 @@ | |
| import org.opensearch.dataprepper.plugins.source.s3.configuration.S3SelectOptions; | ||
| import org.opensearch.dataprepper.plugins.source.s3.configuration.SqsOptions; | ||
| import org.opensearch.dataprepper.plugins.source.s3.configuration.S3DataSelection; | ||
| import org.opensearch.dataprepper.plugins.source.s3.configuration.S3ScanKeyPathOption; | ||
|
|
||
| import java.time.Duration; | ||
| import java.util.Collections; | ||
| import java.util.Map; | ||
|
|
||
| public class S3SourceConfig { | ||
|
|
@@ -104,6 +106,10 @@ public class S3SourceConfig { | |
| @JsonProperty("data_selection") | ||
| private S3DataSelection dataSelection = S3DataSelection.DATA_AND_METADATA; | ||
|
|
||
| @JsonProperty("filters") | ||
| @Valid | ||
| private Map<String, S3ScanKeyPathOption> filters; | ||
|
|
||
| @AssertTrue(message = "A codec is required for reading objects.") | ||
| boolean isCodecProvidedWhenNeeded() { | ||
| if(s3SelectOptions == null) | ||
|
|
@@ -139,6 +145,19 @@ boolean isS3SelectNotUsingDeleteS3ObjectsOnRead() { | |
| return true; | ||
| } | ||
|
|
||
| @AssertTrue(message = "Top-level filters cannot be used together with scan bucket-level filter. Use one or the other.") | ||
| boolean isFiltersNotUsedWithScanBucketFilter() { | ||
|
Collaborator
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. nit: Early returns (guard clauses) to reduce nesting for better readability.
Contributor
Author
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. Refactored to use guard clauses. Thanks! |
||
| if (filters == null || filters.isEmpty()) { | ||
| return true; | ||
| } | ||
| if (s3ScanScanOptions == null || s3ScanScanOptions.getBuckets() == null) { | ||
| return true; | ||
| } | ||
| return s3ScanScanOptions.getBuckets().stream() | ||
| .map(bucket -> bucket.getS3ScanBucketOption()) | ||
| .noneMatch(option -> option != null && option.getS3ScanFilter() != null); | ||
| } | ||
|
|
||
| public NotificationTypeOption getNotificationType() { | ||
| return notificationType; | ||
| } | ||
|
|
@@ -222,4 +241,8 @@ public boolean isDeleteS3MetadataInEvent() { | |
| public S3DataSelection getDataSelection() { | ||
| return dataSelection; | ||
| } | ||
|
|
||
| public Map<String, S3ScanKeyPathOption> getFilters() { | ||
| return filters != null ? filters : Collections.emptyMap(); | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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
101 changes: 101 additions & 0 deletions
101
...rce/src/test/java/org/opensearch/dataprepper/plugins/source/s3/S3ObjectKeyFilterTest.java
This file contains hidden or 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,101 @@ | ||
| /* | ||
| * Copyright OpenSearch Contributors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| * | ||
| * The OpenSearch Contributors require contributions made to | ||
| * this file be licensed under the Apache-2.0 license or a | ||
| * compatible open source license. | ||
| * | ||
| */ | ||
|
|
||
| package org.opensearch.dataprepper.plugins.source.s3; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.params.ParameterizedTest; | ||
| import org.junit.jupiter.params.provider.CsvSource; | ||
| import org.opensearch.dataprepper.plugins.source.s3.configuration.S3ScanKeyPathOption; | ||
|
|
||
| import java.util.Collections; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| import static org.hamcrest.CoreMatchers.equalTo; | ||
| import static org.hamcrest.MatcherAssert.assertThat; | ||
| import static org.mockito.Mockito.mock; | ||
| import static org.mockito.Mockito.when; | ||
|
|
||
| class S3ObjectKeyFilterTest { | ||
|
|
||
| @Test | ||
| void isKeyMatchingFilters_returns_true_when_filters_is_empty() { | ||
| final S3ObjectKeyFilter filter = new S3ObjectKeyFilter(Collections.emptyMap()); | ||
| assertThat(filter.isKeyMatchingFilters("my-bucket", "any/key.json"), equalTo(true)); | ||
| } | ||
|
|
||
| @Test | ||
| void isKeyMatchingFilters_returns_true_when_filters_is_null() { | ||
| final S3ObjectKeyFilter filter = new S3ObjectKeyFilter(null); | ||
| assertThat(filter.isKeyMatchingFilters("my-bucket", "any/key.json"), equalTo(true)); | ||
| } | ||
|
|
||
| @Test | ||
| void isKeyMatchingFilters_returns_true_when_bucket_not_in_filters() { | ||
| final S3ScanKeyPathOption keyPathOption = mock(S3ScanKeyPathOption.class); | ||
| when(keyPathOption.getS3scanIncludePrefixOptions()).thenReturn(List.of("assets/")); | ||
| final S3ObjectKeyFilter filter = new S3ObjectKeyFilter(Map.of("other-bucket", keyPathOption)); | ||
|
|
||
| assertThat(filter.isKeyMatchingFilters("my-bucket", "any/key.json"), equalTo(true)); | ||
| } | ||
|
|
||
| @ParameterizedTest | ||
| @CsvSource({ | ||
| "assets/image.png, true", | ||
| "logs/app.log, false" | ||
| }) | ||
| void isKeyMatchingFilters_with_include_prefix(final String objectKey, final boolean expected) { | ||
| final S3ScanKeyPathOption keyPathOption = mock(S3ScanKeyPathOption.class); | ||
| when(keyPathOption.getS3scanIncludePrefixOptions()).thenReturn(List.of("assets/")); | ||
| when(keyPathOption.getS3ScanExcludeSuffixOptions()).thenReturn(null); | ||
| final S3ObjectKeyFilter filter = new S3ObjectKeyFilter(Map.of("my-bucket", keyPathOption)); | ||
|
|
||
| assertThat(filter.isKeyMatchingFilters("my-bucket", objectKey), equalTo(expected)); | ||
| } | ||
|
|
||
| @ParameterizedTest | ||
| @CsvSource({ | ||
| "assets/photo.jpg, false", | ||
| "assets/data.json, true" | ||
| }) | ||
| void isKeyMatchingFilters_with_exclude_suffix(final String objectKey, final boolean expected) { | ||
| final S3ScanKeyPathOption keyPathOption = mock(S3ScanKeyPathOption.class); | ||
| when(keyPathOption.getS3scanIncludePrefixOptions()).thenReturn(null); | ||
| when(keyPathOption.getS3ScanExcludeSuffixOptions()).thenReturn(List.of(".jpg", ".xml")); | ||
| final S3ObjectKeyFilter filter = new S3ObjectKeyFilter(Map.of("my-bucket", keyPathOption)); | ||
|
|
||
| assertThat(filter.isKeyMatchingFilters("my-bucket", objectKey), equalTo(expected)); | ||
| } | ||
|
|
||
| @ParameterizedTest | ||
| @CsvSource({ | ||
| "assets/data.json, true", | ||
| "assets/photo.jpg, false", | ||
| "logs/app.log, false" | ||
| }) | ||
| void isKeyMatchingFilters_with_both_include_prefix_and_exclude_suffix(final String objectKey, final boolean expected) { | ||
| final S3ScanKeyPathOption keyPathOption = mock(S3ScanKeyPathOption.class); | ||
| when(keyPathOption.getS3scanIncludePrefixOptions()).thenReturn(List.of("assets/")); | ||
| when(keyPathOption.getS3ScanExcludeSuffixOptions()).thenReturn(List.of(".jpg")); | ||
| final S3ObjectKeyFilter filter = new S3ObjectKeyFilter(Map.of("my-bucket", keyPathOption)); | ||
|
|
||
| assertThat(filter.isKeyMatchingFilters("my-bucket", objectKey), equalTo(expected)); | ||
| } | ||
|
|
||
| @Test | ||
| void isKeyMatchingFilters_returns_true_when_key_matches_any_include_prefix() { | ||
| final S3ScanKeyPathOption keyPathOption = mock(S3ScanKeyPathOption.class); | ||
| when(keyPathOption.getS3scanIncludePrefixOptions()).thenReturn(List.of("assets/", "data/")); | ||
| final S3ObjectKeyFilter filter = new S3ObjectKeyFilter(Map.of("my-bucket", keyPathOption)); | ||
|
|
||
| assertThat(filter.isKeyMatchingFilters("my-bucket", "data/file.csv"), equalTo(true)); | ||
| } | ||
| } |
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
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.
Thank you for this assertion. It is the right approach in my opinion.
Uh oh!
There was an error while loading. Please reload this page.
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.
May I ask why we don't want to keep filters at both levels. For the case below that includes difference filters at two bucket level, it couldn't support SQS as the notification with a top level filter.
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.
@Zhangxunmt , The top-level filters are still able to differentiate buckets even with SQS. SQS includes the bucket name in the message.