Skip to content

fix(s3): filter objects during pagination in S3Service.list to avoid premature loop exit - #853

Merged
Malaydewangan09 merged 4 commits into
kestra-io:mainfrom
ADITYASINGH1206:fix/851-s3-pagination-filter-break
Oct 26, 2026
Merged

Malaydewangan09 merged 4 commits into
kestra-io:mainfrom
ADITYASINGH1206:fix/851-s3-pagination-filter-break

Conversation

@ADITYASINGH1206

Copy link
Copy Markdown
Contributor

Closes #851.

What problem does this PR solve?

In S3Service.list(), pagination across S3 pages evaluates the termination condition allContents.size() >= rMaxKeys against raw, unfiltered S3 objects before applying regExp and filter. When an early page contains objects that do not match the filter (for instance, non-matching files under a common prefix), the loop breaks prematurely once the raw count reaches rMaxKeys, silently dropping matching objects that reside on subsequent pages.

What changes are made in this PR?

  1. Evaluates S3Service.filter(s3Object, regExp, filter) iteratively as each page's contents are received from S3.
  2. Directly collects matching objects into the result list, terminating early only when the matching count satisfies rMaxKeys.
  3. Continues requesting subsequent pages via continuationToken whenever the current page does not provide enough matching objects and response.isTruncated() is true.
  4. Added unit tests in S3ServiceListTest.java:
    • shouldContinuePaginationWhenInitialPageDoesNotMatchFilter(): verifies that non-matching objects on page 1 do not prematurely stop pagination, allowing matching objects on page 2 to be returned.
    • shouldStopPaginationWhenMatchingCountReachesMaxKeys(): verifies that pagination stops immediately once the requested maxKeys quota of matching objects is reached.

@kestrabot kestrabot Bot added area/plugin Plugin-related issue or feature request kind/external Pull requests raised by community contributors labels Oct 22, 2026
@github-project-automation github-project-automation Bot moved this to To review in Pull Requests Oct 22, 2026
@kestrabot
kestrabot Bot requested a review from a team October 22, 2026 12:51
@kestrabot
kestrabot Bot requested a review from Malaydewangan09 October 22, 2026 12:51

@knowprasad knowprasad left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @ADITYASINGH1206,

Took a look through your PR and really liked this fix. That pagination bug was subtle: if the first S3 page was full of files that did not match the filter, the old code was bailing out way too early and dropping files on later pages.

Filtering per page as the chunks come in rather than hoarding all raw objects in memory first is so much cleaner and avoids unnecessary memory allocations. Also, asserting verify(mockClient, times(1)) in the unit test to confirm it stops as soon as maxKeys is hit was a really nice touch.

Code looks super solid and clean to me. Hope the maintainers get a chance to review and merge it soon!

@ADITYASINGH1206
ADITYASINGH1206 force-pushed the fix/851-s3-pagination-filter-break branch from 2ff380d to de8966c Compare October 25, 2026 05:34
@ADITYASINGH1206

Copy link
Copy Markdown
Contributor Author

Thank you @knowprasad for the thorough review and kind words! Glad you liked the per-page streaming filter and maxKeys verification.

@Malaydewangan09

Malaydewangan09 commented Oct 26, 2026 •

Copy link
Copy Markdown
Member

Thanks for the fix, filtering each page is the right way to solve #851.

I made a few small changes on top. With a regexp or filter set, it now fetches full pages of 1000 keys. maxKeys 0 returns nothing without calling S3, and a negative value gives a clear error.

One thing to know: DeleteList and Trigger now search the whole prefix for matches, not just the first page.

I pushed these tweaks.

@Malaydewangan09
Malaydewangan09 merged commit 4edae9c into kestra-io:main Oct 26, 2026
4 checks passed
@github-project-automation github-project-automation Bot moved this from To review to Done in Pull Requests Oct 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/plugin Plugin-related issue or feature request kind/external Pull requests raised by community contributors

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

s3.List / S3Service.list: premature pagination break drops matching files across pages

3 participants