/
ListObjectsV2Paginator.ts
58 lines (57 loc) · 1.75 KB
/
ListObjectsV2Paginator.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import { S3 } from "../S3";
import { S3Client } from "../S3Client";
import {
ListObjectsV2Command,
ListObjectsV2CommandInput,
ListObjectsV2CommandOutput,
} from "../commands/ListObjectsV2Command";
import { S3PaginationConfiguration } from "./Interfaces";
import { Paginator } from "@aws-sdk/types";
/**
* @private
*/
const makePagedClientRequest = async (
client: S3Client,
input: ListObjectsV2CommandInput,
...args: any
): Promise<ListObjectsV2CommandOutput> => {
// @ts-ignore
return await client.send(new ListObjectsV2Command(input), ...args);
};
/**
* @private
*/
const makePagedRequest = async (
client: S3,
input: ListObjectsV2CommandInput,
...args: any
): Promise<ListObjectsV2CommandOutput> => {
// @ts-ignore
return await client.listObjectsV2(input, ...args);
};
export async function* paginateListObjectsV2(
config: S3PaginationConfiguration,
input: ListObjectsV2CommandInput,
...additionalArguments: any
): Paginator<ListObjectsV2CommandOutput> {
// ToDo: replace with actual type instead of typeof input.ContinuationToken
let token: typeof input.ContinuationToken | undefined = config.startingToken || undefined;
let hasNext = true;
let page: ListObjectsV2CommandOutput;
while (hasNext) {
input.ContinuationToken = token;
input["MaxKeys"] = config.pageSize;
if (config.client instanceof S3) {
page = await makePagedRequest(config.client, input, ...additionalArguments);
} else if (config.client instanceof S3Client) {
page = await makePagedClientRequest(config.client, input, ...additionalArguments);
} else {
throw new Error("Invalid client, expected S3 | S3Client");
}
yield page;
token = page.NextContinuationToken;
hasNext = !!token;
}
// @ts-ignore
return undefined;
}