-
-
Notifications
You must be signed in to change notification settings - Fork 24.7k
fix: paginate S3 directory listings #6630
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
base: main
Are you sure you want to change the base?
Changes from all commits
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,47 @@ | ||
| jest.mock('@aws-sdk/client-s3', () => ({ | ||
| S3Client: jest.fn(), | ||
| GetObjectCommand: jest.fn(), | ||
| ListObjectsV2Command: jest.fn().mockImplementation((input) => ({ input })) | ||
| })) | ||
|
|
||
| describe('S3Directory', () => { | ||
| const s3Client = { send: jest.fn() } | ||
| let getS3FileKeys: (s3Client: any, bucketName: string, prefix: string) => Promise<string[]> | ||
| let ListObjectsV2Command: jest.Mock | ||
|
|
||
| beforeEach(async () => { | ||
| jest.clearAllMocks() | ||
| s3Client.send.mockReset() | ||
|
|
||
| const s3Module = require('@aws-sdk/client-s3') | ||
| ListObjectsV2Command = s3Module.ListObjectsV2Command | ||
|
|
||
| const module = (await import('./S3Directory')) as any | ||
| getS3FileKeys = module.getS3FileKeys | ||
| }) | ||
|
|
||
| it('loads keys from every S3 list page', async () => { | ||
| s3Client.send | ||
| .mockResolvedValueOnce({ | ||
| Contents: [{ Key: 'docs/0001.txt', ETag: 'etag-1' }], | ||
| IsTruncated: true, | ||
| NextContinuationToken: 'next-page' | ||
| }) | ||
| .mockResolvedValueOnce({ | ||
| Contents: [{ Key: 'docs/1001.txt', ETag: 'etag-2' }], | ||
| IsTruncated: false | ||
| }) | ||
|
|
||
| await expect(getS3FileKeys(s3Client, 'documents', 'docs/')).resolves.toEqual(['docs/0001.txt', 'docs/1001.txt']) | ||
| expect(ListObjectsV2Command).toHaveBeenNthCalledWith(1, { | ||
| Bucket: 'documents', | ||
| Prefix: 'docs/', | ||
| ContinuationToken: undefined | ||
| }) | ||
| expect(ListObjectsV2Command).toHaveBeenNthCalledWith(2, { | ||
| Bucket: 'documents', | ||
| Prefix: 'docs/', | ||
| ContinuationToken: 'next-page' | ||
| }) | ||
| }) | ||
| }) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,6 +18,27 @@ import { TextSplitter } from '@langchain/textsplitters' | |
| import { CSVLoader } from '../Csv/CsvLoader' | ||
| import { LoadOfSheet } from '../MicrosoftExcel/ExcelLoader' | ||
| import { PowerpointLoader } from '../MicrosoftPowerpoint/PowerpointLoader' | ||
|
|
||
| const getS3FileKeys = async (s3Client: S3Client, bucketName: string, prefix: string): Promise<string[]> => { | ||
| const keys: string[] = [] | ||
| let continuationToken: string | undefined | ||
|
|
||
| do { | ||
| const listObjectsOutput: ListObjectsV2Output = await s3Client.send( | ||
| new ListObjectsV2Command({ | ||
| Bucket: bucketName, | ||
| Prefix: prefix, | ||
| ContinuationToken: continuationToken | ||
| }) | ||
| ) | ||
|
|
||
| keys.push(...(listObjectsOutput.Contents ?? []).filter((item) => item.Key && item.ETag).map((item) => item.Key!)) | ||
| continuationToken = listObjectsOutput.IsTruncated ? listObjectsOutput.NextContinuationToken : undefined | ||
| } while (continuationToken) | ||
|
|
||
| return keys | ||
| } | ||
|
|
||
| class S3_DocumentLoaders implements INode { | ||
| label: string | ||
| name: string | ||
|
|
@@ -178,14 +199,7 @@ class S3_DocumentLoaders implements INode { | |
| try { | ||
| const s3Client = new S3Client(s3Config) | ||
|
|
||
| const listObjectsOutput: ListObjectsV2Output = await s3Client.send( | ||
| new ListObjectsV2Command({ | ||
| Bucket: bucketName, | ||
| Prefix: prefix | ||
| }) | ||
| ) | ||
|
|
||
| const keys: string[] = (listObjectsOutput?.Contents ?? []).filter((item) => item.Key && item.ETag).map((item) => item.Key!) | ||
| const keys = await getS3FileKeys(s3Client, bucketName, prefix) | ||
|
Contributor
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. With the introduction of pagination in
Consider implementing a concurrency limit (e.g., processing in batches of 10-50 files) to make the download process robust and scalable. |
||
|
|
||
| await Promise.all( | ||
| keys.map(async (key) => { | ||
|
|
@@ -291,4 +305,4 @@ class S3_DocumentLoaders implements INode { | |
| } | ||
| } | ||
| } | ||
| module.exports = { nodeClass: S3_DocumentLoaders } | ||
| module.exports = { nodeClass: S3_DocumentLoaders, getS3FileKeys } | ||
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.
The
prefixparameter is optional in the node inputs (line 104) and can beundefinedat runtime. However, thegetS3FileKeysfunction signature typesprefixas a requiredstring. To maintain type safety and prevent potential compilation or runtime issues under strict null checks, update the parameter type tostring | undefinedor make it optional.