-
Notifications
You must be signed in to change notification settings - Fork 4k
/
Copy pathitem-reader.ts
417 lines (368 loc) · 10.8 KB
/
item-reader.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
import * as iam from '../../../../aws-iam';
import { IBucket } from '../../../../aws-s3';
import { Arn, ArnFormat, Aws } from '../../../../core';
import { FieldUtils } from '../../fields';
/**
* Base interface for Item Reader configurations
*/
export interface IItemReader {
/**
* S3 Bucket containing objects to iterate over or a file with a list to iterate over
*/
readonly bucket: IBucket;
/**
* S3 bucket name containing objects to iterate over or a file with a list to iterate over, as JsonPath
*/
readonly bucketNamePath?: string;
/**
* The Amazon S3 API action that Step Functions must invoke depending on the specified dataset.
*/
readonly resource: string;
/**
* Limits the number of items passed to the Distributed Map state
*
* @default - Distributed Map state will iterate over all items provided by the ItemReader
*/
readonly maxItems?: number;
/**
* Render the ItemReader as JSON object
*/
render(): any;
/**
* Compile policy statements to provide relevent permissions to the state machine
*/
providePolicyStatements(): iam.PolicyStatement[];
/**
* Validate that ItemReader contains exactly either @see bucket or @see bucketNamePath
*/
validateItemReader(): string[];
}
/**
* Base interface for Item Reader configuration properties
*/
export interface ItemReaderProps {
/**
* S3 Bucket containing objects to iterate over or a file with a list to iterate over
*
* @default - S3 bucket will be determined from @see bucketNamePath
*/
readonly bucket?: IBucket;
/**
* S3 bucket name containing objects to iterate over or a file with a list to iterate over, as JsonPath
*
* @default - S3 bucket will be determined from @see bucket
*/
readonly bucketNamePath?: string;
/**
* Limits the number of items passed to the Distributed Map state
*
* @default - Distributed Map state will iterate over all items provided by the ItemReader
*/
readonly maxItems?: number;
}
/**
* Properties for configuring an Item Reader that iterates over objects in an S3 bucket
*/
export interface S3ObjectsItemReaderProps extends ItemReaderProps {
/**
* S3 prefix used to limit objects to iterate over
*
* @default - No prefix
*/
readonly prefix?: string;
}
/**
* Item Reader configuration for iterating over objects in an S3 bucket
*/
export class S3ObjectsItemReader implements IItemReader {
private readonly _bucket?: IBucket;
/**
* S3 Bucket containing objects to iterate over
*/
public get bucket(): IBucket {
if (!this._bucket) {
throw new Error('`bucket` is undefined');
}
return this._bucket;
}
/**
* S3 bucket name containing objects to iterate over or a file with a list to iterate over, as JsonPath
*/
readonly bucketNamePath?: string;
/**
* ARN for the `listObjectsV2` method of the S3 API
* This API method is used to iterate all objects in the S3 bucket/prefix
*/
readonly resource: string;
/**
* S3 prefix used to limit objects to iterate over
*
* @default - No prefix
*/
readonly prefix?: string;
/**
* Limits the number of items passed to the Distributed Map state
*
* @default - Distributed Map state will iterate over all items provided by the ItemReader
*/
readonly maxItems?: number;
constructor(props: S3ObjectsItemReaderProps) {
this._bucket = props.bucket;
this.bucketNamePath = props.bucketNamePath;
this.prefix = props.prefix;
this.maxItems = props.maxItems;
this.resource = Arn.format({
region: '',
account: '',
partition: Aws.PARTITION,
service: 'states',
resource: 's3',
resourceName: 'listObjectsV2',
arnFormat: ArnFormat.COLON_RESOURCE_NAME,
});
}
/**
* Renders the ItemReader configuration as JSON object
* @returns - JSON object
*/
public render(): any {
return FieldUtils.renderObject({
Resource: this.resource,
...(this.maxItems && { ReaderConfig: { MaxItems: this.maxItems } }),
Parameters: {
...(this._bucket && { Bucket: this._bucket.bucketName }),
...(this.bucketNamePath && { Bucket: this.bucketNamePath }),
...(this.prefix && { Prefix: this.prefix }),
},
});
}
/**
* Compile policy statements to provide relevent permissions to the state machine
*/
public providePolicyStatements(): iam.PolicyStatement[] {
return [
new iam.PolicyStatement({
actions: [
's3:ListBucket',
],
resources: [this._bucket ? this._bucket.bucketArn : '*'],
}),
];
}
/**
* Validate that ItemReader contains exactly either @see bucket or @see bucketNamePath
*/
public validateItemReader(): string[] {
const errors: string[] = [];
if (this._bucket && this.bucketNamePath) {
errors.push('Provide either `bucket` or `bucketNamePath`, but not both');
} else if (!this._bucket && !this.bucketNamePath) {
errors.push('Provide either `bucket` or `bucketNamePath`');
}
return errors;
}
}
/**
* Base interface for Item Reader configuration properties the iterate over entries in a S3 file
*/
export interface S3FileItemReaderProps extends ItemReaderProps {
/**
* Key of file stored in S3 bucket containing an array to iterate over
*/
readonly key: string;
}
/**
* Base Item Reader configuration for iterating over entries in a S3 file
*/
abstract class S3FileItemReader implements IItemReader {
private readonly _bucket?: IBucket;
/**
* S3 Bucket containing a file with a list to iterate over
*/
public get bucket(): IBucket {
if (!this._bucket) {
throw new Error('`bucket` is undefined');
}
return this._bucket;
}
/**
* S3 bucket name containing objects to iterate over or a file with a list to iterate over, as JsonPath
*/
readonly bucketNamePath?: string;
/**
* S3 key of a file with a list to iterate over
*/
readonly key: string;
/**
* ARN for the `getObject` method of the S3 API
* This API method is used to iterate all objects in the S3 bucket/prefix
*/
readonly resource: string;
/**
* Limits the number of items passed to the Distributed Map state
*
* @default - No maxItems
*/
readonly maxItems?: number;
protected abstract readonly inputType: string;
constructor(props: S3FileItemReaderProps) {
this._bucket = props.bucket;
this.bucketNamePath = props.bucketNamePath;
this.key = props.key;
this.maxItems = props.maxItems;
this.resource = Arn.format({
region: '',
account: '',
partition: Aws.PARTITION,
service: 'states',
resource: 's3',
resourceName: 'getObject',
arnFormat: ArnFormat.COLON_RESOURCE_NAME,
});
}
/**
* Renders the ItemReader configuration as JSON object
* @returns - JSON object
*/
public render(): any {
return FieldUtils.renderObject({
Resource: this.resource,
ReaderConfig: {
InputType: this.inputType,
...(this.maxItems && { MaxItems: this.maxItems }),
},
Parameters: {
...(this._bucket && { Bucket: this._bucket.bucketName }),
...(this.bucketNamePath && { Bucket: this.bucketNamePath }),
Key: this.key,
},
});
}
/**
* Compile policy statements to provide relevent permissions to the state machine
*/
public providePolicyStatements(): iam.PolicyStatement[] {
if (!this._bucket) return [];
const resource = Arn.format({
region: '',
account: '',
partition: Aws.PARTITION,
service: 's3',
resource: this._bucket.bucketName,
resourceName: '*',
});
return [
new iam.PolicyStatement({
actions: [
's3:GetObject',
],
resources: [resource],
}),
];
}
/**
* Validate that ItemReader contains exactly either @see bucket or @see bucketNamePath
*/
public validateItemReader(): string[] {
const errors: string[] = [];
if (this._bucket && this.bucketNamePath) {
errors.push('Provide either `bucket` or `bucketNamePath`, but not both');
} else if (!this._bucket && !this.bucketNamePath) {
errors.push('Provide either `bucket` or `bucketNamePath`');
}
return errors;
}
}
/**
* Item Reader configuration for iterating over items in a JSON array stored in a S3 file
*/
export class S3JsonItemReader extends S3FileItemReader {
protected readonly inputType: string = 'JSON';
}
/**
* CSV header location options
*/
export enum CsvHeaderLocation {
/**
* Headers will be read from first row of CSV file
*/
FIRST_ROW = 'FIRST_ROW',
/**
* Headers are provided in CSVHeaders property
*/
GIVEN = 'GIVEN',
}
/**
* Configuration for CSV header options for a CSV Item Reader
*/
export class CsvHeaders {
/**
* Configures S3CsvItemReader to read headers from the first row of the CSV file
* @returns - CsvHeaders
*/
public static useFirstRow(): CsvHeaders {
return new CsvHeaders(CsvHeaderLocation.FIRST_ROW);
}
/**
* Configures S3CsvItemReader to use the headers provided in the `headers` parameter
* @param headers - List of headers
* @returns - CsvHeaders
*/
public static use(headers: string[]): CsvHeaders {
return new CsvHeaders(CsvHeaderLocation.GIVEN, headers);
}
/**
* Location of headers in CSV file
*/
public readonly headerLocation: CsvHeaderLocation;
/**
* List of headers if `headerLocation` is `GIVEN`
*/
public readonly headers?: string[];
private constructor(headerLocation: CsvHeaderLocation, headers?: string[]) {
this.headerLocation = headerLocation;
this.headers = headers;
}
}
/**
* Properties for configuring an Item Reader that iterates over items in a CSV file in S3
*/
export interface S3CsvItemReaderProps extends S3FileItemReaderProps {
/**
* CSV file header configuration
*
* @default - CsvHeaders with CsvHeadersLocation.FIRST_ROW
*/
readonly csvHeaders?: CsvHeaders;
}
/**
* Item Reader configuration for iterating over items in a CSV file stored in S3
*/
export class S3CsvItemReader extends S3FileItemReader {
/**
* CSV headers configuration
*/
readonly csvHeaders: CsvHeaders;
protected readonly inputType: string = 'CSV';
constructor(props: S3CsvItemReaderProps) {
super(props);
this.csvHeaders = props.csvHeaders ?? CsvHeaders.useFirstRow();
}
public render(): any {
let rendered = super.render();
rendered.ReaderConfig = FieldUtils.renderObject({
...rendered.ReaderConfig,
...{
CSVHeaderLocation: this.csvHeaders.headerLocation,
...(this.csvHeaders.headers && { CSVHeaders: this.csvHeaders.headers }),
},
});
return rendered;
}
}
/**
* Item Reader configuration for iterating over items in a S3 inventory manifest file stored in S3
*/
export class S3ManifestItemReader extends S3FileItemReader {
protected readonly inputType: string = 'MANIFEST';
}