-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAsyncTextract.ts
687 lines (615 loc) · 20.7 KB
/
AsyncTextract.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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
import * as aws from '@pulumi/aws'
import * as pulumi from '@pulumi/pulumi'
import * as AWS from 'aws-sdk'
import {
LambdaCloudWatchPolicy,
S3ReadWritePolicy,
SNSPublishPolicy,
SQSProcessPolicy,
TextractPolicy
} from './policies'
import { S3NotificationQueue } from './S3NotificationQueue'
import { SNSEventsQueue } from './SNSEventsQueue'
import { PromiseResult } from 'aws-sdk/lib/request'
import { extname } from 'path'
export interface TextExtractorArgs {
/**
* Bucket to use for the events.
*
* If omitted, a new s3 bucket will be created.
*/
bucket?: aws.s3.Bucket
/**
* File formats that need to be handled.
*
* By default all supported file formats will be used.
*/
fileFormats?: ('jpeg' | 'png' | 'pdf')[]
/**
* Available Operations:
* - `StartDocumentTextDetection` (https://docs.aws.amazon.com/textract/latest/dg/API_StartDocumentTextDetection.html)
* - `StartDocumentAnalysis` (https://docs.aws.amazon.com/textract/latest/dg/API_StartDocumentAnalysis.html)
*
* Default operation is `StartDocumentTextDetection`
*/
operation?: 'StartDocumentAnalysis' | 'StartDocumentTextDetection'
/**
* A list of the types of analysis to perform.
* Only used if `operation` is `StartDocumentAnalysis`. If omitted, defaults to ["TABLES", "FORMS"].
* https://docs.aws.amazon.com/textract/latest/dg/API_StartDocumentAnalysis.html#API_StartDocumentAnalysis_RequestSyntax
*/
featureTypes?: AWS.Textract.FeatureTypes
}
/**
* Configures Amazon Textract pipeline for Asynchronous Operations.
*
* In the simplest form;
* ```typescript
* import { AsyncTextract } from 'pulumi-aws-components'
*
* // To extract text from PDF, JPEG, PNG
* const textExtract = new AsyncTextract('text-extractor', {})
*
* // To extract text from PDF only
* const pdfTextExtractor = new new AsyncTextract('text-extractor-pdf', {
* fileFormats: ['pdf']
* })
* ```
*/
export class AsyncTextract extends pulumi.ComponentResource {
/**
* Service role used for entire pipeline
*/
readonly role: aws.iam.Role
/**
* Source bucket for the pipeline to trigger events against
*/
readonly bucket: aws.s3.Bucket
/**
* Queue that subscribes to bucket events
*/
readonly s3NotificationQueue: S3NotificationQueue
/**
* Lambda that processes messages from bucket events queue (i.e. `s3NotificationQueue`)
*/
readonly s3NotificationQueueProcessingLambda: aws.lambda.CallbackFunction<aws.sqs.QueueEvent, void>
/**
* SNS topic that will be used by Textract to send job status notifications
*/
readonly jobStatusNotificationTopic: aws.sns.Topic
/**
* Queue that subscribes to SNS `jobStatusNotificationTopic`
*/
readonly jobStatusNotificationQueue: SNSEventsQueue
/**
* Lambda that processes messages from job status queue (i.e. `jobStatusNotificationQueue`)
*/
readonly jobResultProcessingLambda: aws.lambda.CallbackFunction<aws.sqs.QueueEvent, void>
readonly bucketReadWritePolicy: S3ReadWritePolicy
readonly s3NotificationQueueProcessPolicy: SQSProcessPolicy
readonly jobStatusNotificationQueueProcessPolicy: SQSProcessPolicy
readonly jobStatusPublishPolicy: SNSPublishPolicy
readonly textractPolicy: TextractPolicy
/**
* Creates an Amazon Textract pipeline from a bucket event.
* Default operation is `StartDocumentTextDetection`
* (https://docs.aws.amazon.com/textract/latest/dg/API_StartDocumentTextDetection.html)
*
* @param name The _unique_ name of the resource.
* @param args The arguments to configure the TextExtractor.
* @param opts A bag of options that control this resource's behavior.
*/
constructor(name: string, args: TextExtractorArgs, opts?: pulumi.CustomResourceOptions) {
super('aws:components:AsyncTextExtract', name, args, opts)
// Default resource options for this component's child resources.
const defaultResourceOptions: pulumi.ResourceOptions = { parent: this }
// Role
const roleName = `${name}-ServiceRole`
this.role = new aws.iam.Role(
roleName,
{
name: roleName,
assumeRolePolicy: aws.iam.assumeRolePolicyForPrincipal({
Service: ['textract.amazonaws.com', 'lambda.amazonaws.com']
})
},
defaultResourceOptions
)
// S3 Bucket and it's policies
const bucketName = `${name.toLowerCase()}-bucket`
const {
operation = 'StartDocumentTextDetection',
bucket = new aws.s3.Bucket(
bucketName,
{
bucket: bucketName,
serverSideEncryptionConfiguration: {
rule: {
applyServerSideEncryptionByDefault: {
sseAlgorithm: 'AES256'
}
}
}
},
defaultResourceOptions
)
} = args
this.bucket = bucket
// SNS Topic for the job status / result notification
const jobStatusNotificationTopicName = `${name}-job-status`
this.jobStatusNotificationTopic = new aws.sns.Topic(
jobStatusNotificationTopicName,
{
name: jobStatusNotificationTopicName
},
defaultResourceOptions
)
// S3 Notification Queue
const fileFormats: string[] = args.fileFormats || ['pdf', 'jpeg', 'png']
if (fileFormats.includes('jpeg')) {
fileFormats.push('jpg')
}
this.s3NotificationQueue = new S3NotificationQueue(
`${name}-s3-notification-queue`,
{
bucket: this.bucket,
events: ['s3:ObjectCreated:*'],
notificationFilterRules: fileFormats.map(format => ({
filterSuffix: `.${format}`
}))
},
{ ...defaultResourceOptions, dependsOn: this.bucket }
)
// Lambda handler to process S3 Notifications
const isTextDetection = operation === 'StartDocumentTextDetection'
const lambdaName = `${name}-s3-notification-lambda`
this.s3NotificationQueueProcessingLambda = new aws.lambda.CallbackFunction(
lambdaName,
{
name: lambdaName,
callback: startTextExtractionHandler,
role: this.role,
runtime: aws.lambda.NodeJS12dXRuntime,
timeout: 30,
environment: {
variables: {
ROLE_ARN: this.role.arn,
S3_NOTIFICATION_QUEUE_URL: this.s3NotificationQueue.queue.id,
SNS_TOPIC_JOB_STATUS_ARN: this.jobStatusNotificationTopic.arn,
TEXTRACT_API: operation,
...(!isTextDetection && args.featureTypes && args.featureTypes.length
? { ANALYSIS_FEATURE_TYPES: args.featureTypes.join(',') }
: {})
}
}
},
defaultResourceOptions
)
// Job Status queue subscribed to SNS topic `jobStatusNotificationTopic`
const visibilityTimeoutSeconds = 60
this.jobStatusNotificationQueue = new SNSEventsQueue(
`${jobStatusNotificationTopicName}-job-status-notification-queue`,
{
topic: this.jobStatusNotificationTopic,
visibilityTimeoutSeconds
},
defaultResourceOptions
)
// Lambda handler to process results
const jobResultProcessingLambdaName = `${name}-job-result-processing-lambda`
this.jobResultProcessingLambda = new aws.lambda.CallbackFunction(
jobResultProcessingLambdaName,
{
name: jobResultProcessingLambdaName,
callback: textractJobResultProcessor,
role: this.role,
runtime: aws.lambda.NodeJS12dXRuntime,
timeout: visibilityTimeoutSeconds,
environment: {
variables: {
JOB_STATUS_QUEUE_URL: this.jobStatusNotificationQueue.queue.id
}
}
},
defaultResourceOptions
)
// Permissions for assumed role
this.bucketReadWritePolicy = new S3ReadWritePolicy(
`${name}-s3-policy`,
{ bucketArn: this.bucket.arn },
{ ...defaultResourceOptions, dependsOn: this.bucket }
)
new aws.iam.RolePolicyAttachment(
`${name}-s3-policy-attachment`,
{
policyArn: this.bucketReadWritePolicy.policy.arn,
role: this.role
},
{ ...defaultResourceOptions, dependsOn: this.bucketReadWritePolicy }
)
this.jobStatusPublishPolicy = new SNSPublishPolicy(
`${jobStatusNotificationTopicName}-publish-policy`,
{
topicArn: this.jobStatusNotificationTopic.arn
},
{ ...defaultResourceOptions, dependsOn: this.jobStatusNotificationTopic }
)
new aws.iam.RolePolicyAttachment(
`${jobStatusNotificationTopicName}-publish-policy-attachment`,
{
policyArn: this.jobStatusPublishPolicy.policy.arn,
role: this.role
},
{ ...defaultResourceOptions, dependsOn: this.jobStatusPublishPolicy }
)
this.s3NotificationQueueProcessPolicy = new SQSProcessPolicy(
`${name}-s3-notification-queue-process-policy`,
{ queueArn: this.s3NotificationQueue.queue.arn },
{ ...defaultResourceOptions, dependsOn: this.s3NotificationQueue.queue }
)
new aws.iam.RolePolicyAttachment(
`${name}-s3-notification-queue-process-policy-attach`,
{
policyArn: this.s3NotificationQueueProcessPolicy.policy.arn,
role: this.role
},
{ ...defaultResourceOptions, dependsOn: this.s3NotificationQueueProcessPolicy }
)
const cloudwatchPolicy = new LambdaCloudWatchPolicy(
`${lambdaName}-cloudwatch-policy`,
{ lambdaName },
defaultResourceOptions
)
new aws.iam.RolePolicyAttachment(
`${lambdaName}-cloudwatch-policy-attachment`,
{
policyArn: cloudwatchPolicy.policy.arn,
role: this.role
},
defaultResourceOptions
)
this.jobStatusNotificationQueueProcessPolicy = new SQSProcessPolicy(
`${name}-job-status-queue-process-policy`,
{ queueArn: this.jobStatusNotificationQueue.queue.arn },
{ ...defaultResourceOptions, dependsOn: this.jobStatusNotificationQueue.queue }
)
new aws.iam.RolePolicyAttachment(
`${name}-job-status-queue-process-policy-attach`,
{
policyArn: this.jobStatusNotificationQueueProcessPolicy.policy.arn,
role: this.role
},
{ ...defaultResourceOptions, dependsOn: this.jobStatusNotificationQueueProcessPolicy }
)
const jobResultProcessingLambdaCloudwatchPolicy = new LambdaCloudWatchPolicy(
`${jobResultProcessingLambdaName}-cloudwatch-policy`,
{ lambdaName: jobResultProcessingLambdaName },
defaultResourceOptions
)
new aws.iam.RolePolicyAttachment(
`${jobResultProcessingLambdaName}-cloudwatch-policy-attachment`,
{
policyArn: jobResultProcessingLambdaCloudwatchPolicy.policy.arn,
role: this.role
},
defaultResourceOptions
)
this.textractPolicy = new TextractPolicy(`${name}-textract-policy`, {}, defaultResourceOptions)
new aws.iam.RolePolicyAttachment(
`${name}-textract-policy-attachment`,
{
policyArn: this.textractPolicy.policy.arn,
role: this.role
},
{ ...defaultResourceOptions, dependsOn: this.textractPolicy }
)
// S3 notification queue event subscription
this.s3NotificationQueue.queue.onEvent(
`${name}-queue-event-subscription`,
this.s3NotificationQueueProcessingLambda,
{},
defaultResourceOptions
)
this.jobStatusNotificationQueue.queue.onEvent(
`${name}-job-status-notification-subscription`,
this.jobResultProcessingLambda,
{},
defaultResourceOptions
)
this.registerOutputs({
bucket,
role: this.role,
s3NotificationQueue: this.s3NotificationQueue,
s3NotificationQueueProcessingLambda: this.s3NotificationQueueProcessingLambda,
jobStatusNotificationQueue: this.jobStatusNotificationQueue,
jobResultProcessingLambda: this.jobResultProcessingLambda
})
}
}
interface TextractNotificationMessage {
Event?: 's3:TestEvent'
Status: 'SUCCEEDED' | 'FAILED' | 'ERROR'
JobTag: string
JobId: string
API: 'StartDocumentTextDetection' | 'StartDocumentAnalysis'
Timestamp: number
StatusMessage?: string
DocumentLocation: {
S3ObjectName: string
S3Bucket: string
}
}
async function getResults(JobId: string, api: TextractNotificationMessage['API']) {
const textract = new AWS.Textract()
let NextToken: string | undefined
const blockList: AWS.Textract.Block[] = []
do {
const response =
api === 'StartDocumentTextDetection'
? await textract.getDocumentTextDetection({ JobId, NextToken }).promise()
: await textract.getDocumentAnalysis({ JobId, NextToken }).promise()
NextToken = response.NextToken
if (response.Blocks) {
blockList.push(...response.Blocks)
}
} while (NextToken)
return blockList
}
function extractText(blocks: AWS.Textract.Block[]) {
const pdfText: string[] = []
for (const block of blocks) {
if (block.BlockType === 'LINE') {
if (block.Text) {
pdfText.push(block.Text)
}
}
}
return pdfText
}
function getText(result: AWS.Textract.Block, blocksMap: { [x: string]: AWS.Textract.Block }) {
const text: string[] = []
if (result.Relationships) {
for (const relationship of result.Relationships) {
if (relationship.Type === 'CHILD') {
for (const childId in relationship.Ids) {
const word = blocksMap[childId]
if (word.BlockType === 'WORD') {
text.push(word.Text || '')
} else if (word.BlockType === 'SELECTION_ELEMENT' && word.SelectionStatus === 'SELECTED') {
text.push('X')
}
}
}
}
}
return text.join(' ')
}
function getRowsColumnsMap(tableResult: AWS.Textract.Block, blocksMap: { [x: string]: AWS.Textract.Block }) {
const rows: { [row: number]: { [column: number]: string } } = {}
if (tableResult.Relationships) {
for (const relationship of tableResult.Relationships) {
if (relationship.Type === 'CHILD' && relationship.Ids) {
for (const childId of relationship.Ids) {
const cell = blocksMap[childId]
if (cell.BlockType === 'CELL') {
const rowIndex = cell.RowIndex
const columnIndex = cell.ColumnIndex
if (rowIndex && columnIndex) {
if (rows[rowIndex]) {
rows[rowIndex] = {}
}
rows[rowIndex][columnIndex] = getText(cell, blocksMap)
}
}
}
}
}
}
return rows
}
function extractFormData(blocks: AWS.Textract.Block[]) {
const keyBlocks: { [x: string]: AWS.Textract.Block } = {}
const valueBlocks: { [x: string]: AWS.Textract.Block } = {}
const blockMap: { [x: string]: AWS.Textract.Block } = {}
const formKeyValue: { [x: string]: string | null } = {}
for (const block of blocks) {
if (block.Id) {
blockMap[block.Id] = block
if (block.BlockType === 'KEY_VALUE_SET') {
if (block['EntityTypes'] && block['EntityTypes'].includes('KEY')) {
keyBlocks[block.Id] = block
} else {
valueBlocks[block.Id] = block
}
}
}
}
for (const blockId of Object.keys(keyBlocks)) {
const keyBlock = keyBlocks[blockId]
let valueBlock: AWS.Textract.Block | null = null
if (keyBlock.Relationships) {
for (const relationship of keyBlock.Relationships) {
if (relationship.Type === 'VALUE' && relationship.Ids) {
for (const id of relationship.Ids) {
valueBlock = valueBlocks[id]
}
}
}
}
const key = getText(keyBlock, blockMap)
const value = valueBlock ? getText(valueBlock, blockMap) : null
formKeyValue[key] = value
}
return formKeyValue
}
function extractTables(blocks: AWS.Textract.Block[]) {
const blocksMap: { [x: string]: AWS.Textract.Block } = {}
const tableBlocks: AWS.Textract.Block[] = []
for (const block of blocks) {
if (block.Id) {
blocksMap[block.Id] = block
if (block.BlockType === 'TABLE') {
tableBlocks.push(block)
}
}
}
const csv: string[] = []
let tableId = 1
for (const table of tableBlocks) {
const rows = getRowsColumnsMap(table, blocksMap)
const tableName = `Table_${tableId}`
csv.push(`Table: ${tableName}\n\n`)
for (const row of Object.keys(rows)) {
for (const column of Object.keys(rows[+row])) {
csv.push(rows[+row][+column])
csv.push(',')
}
csv.push('\n')
}
csv.push('\n\n')
tableId += 1
}
if (!csv.length) {
return null
}
csv.push('\n\n\n')
return csv.join('')
}
const startTextExtractionHandler: aws.lambda.Callback<aws.sqs.QueueEvent, void> = async (ev, _, callback) => {
const records = ev.Records || []
const queueUrl = process.env['S3_NOTIFICATION_QUEUE_URL']
const RoleArn = process.env['ROLE_ARN']
const SNSTopicArn = process.env['SNS_TOPIC_JOB_STATUS_ARN']
const textractAPI = process.env['TEXTRACT_API'] || 'StartDocumentTextDetection'
if (!RoleArn || !queueUrl || !SNSTopicArn) {
throw new Error('Required ENV are not present')
}
const extract = new AWS.Textract({ logger: console })
const sqs = new AWS.SQS()
for (const record of records) {
const message: aws.s3.BucketEvent = JSON.parse(record.body)
for (const bucketRecord of message.Records || []) {
const Bucket = bucketRecord.s3.bucket.name
const key = bucketRecord.s3.object.key
const JobTag = key
if (textractAPI === 'StartDocumentTextDetection') {
await extract
.startDocumentTextDetection({
JobTag,
DocumentLocation: {
S3Object: {
Bucket,
Name: key
}
},
NotificationChannel: {
RoleArn,
SNSTopicArn
}
})
.promise()
} else {
await extract
.startDocumentAnalysis({
FeatureTypes: process.env['ANALYSIS_FEATURE_TYPES']
? process.env['ANALYSIS_FEATURE_TYPES'].split(',')
: ['TABLES', 'FORMS'],
JobTag,
DocumentLocation: {
S3Object: {
Bucket,
Name: key
}
},
NotificationChannel: {
RoleArn,
SNSTopicArn
}
})
.promise()
}
}
await sqs
.deleteMessage({
QueueUrl: queueUrl,
ReceiptHandle: record.receiptHandle
})
.promise()
}
callback(undefined, undefined)
}
const textractJobResultProcessor: aws.lambda.Callback<aws.sqs.QueueEvent, void> = async (event, _, callback) => {
const s3 = new AWS.S3()
const sqs = new AWS.SQS()
const queueURL = process.env['JOB_STATUS_QUEUE_URL']
if (!queueURL) {
throw new Error('Required ENV are not present')
}
console.log(event)
for (const Record of event['Records']) {
const ReceiptHandle = Record['receiptHandle']
const notificationMessage: TextractNotificationMessage = JSON.parse(Record.body)
if (
!(notificationMessage.Event && notificationMessage.Event === 's3:TestEvent') &&
notificationMessage.Status === 'SUCCEEDED'
) {
const Bucket = notificationMessage.DocumentLocation.S3Bucket
const documentName = notificationMessage.DocumentLocation.S3ObjectName
const results = await getResults(notificationMessage.JobId, notificationMessage.API)
const extractedText = extractText(results)
const formData = extractFormData(results)
const tablesData = extractTables(results)
const promises: Promise<PromiseResult<AWS.S3.PutObjectOutput, AWS.AWSError>>[] = []
if (extractedText.length) {
promises.push(
s3
.putObject({
Bucket,
Key: `${documentName.slice(0, documentName.indexOf(extname(notificationMessage.JobId)))}-${
notificationMessage.API
}.txt`,
Body: extractedText.join('\n'),
ContentType: 'text/plain'
})
.promise()
)
}
if (formData && Object.keys(formData).length) {
promises.push(
s3
.putObject({
Bucket,
Key: `${documentName.slice(0, documentName.indexOf(extname(notificationMessage.JobId)))}-${
notificationMessage.API
}.json`,
Body: JSON.stringify(formData),
ContentType: 'application/json'
})
.promise()
)
}
if (tablesData) {
promises.push(
s3
.putObject({
Bucket,
Key: `${documentName.slice(0, documentName.indexOf(extname(notificationMessage.JobId)))}-${
notificationMessage.API
}.csv`,
Body: tablesData,
ContentType: 'application/csv'
})
.promise()
)
}
await Promise.all(promises)
}
await sqs
.deleteMessage({
QueueUrl: queueURL,
ReceiptHandle
})
.promise()
callback(undefined, undefined)
}
}