MCPcopy Create free account
hub / github.com/GoogleCloudPlatform/nodejs-docs-samples / main

Function main

document-ai/batch-process-document.js:20–148  ·  view source on GitHub ↗
(
  projectId = 'YOUR_PROJECT_ID',
  location = 'YOUR_PROJECT_LOCATION',
  processorId = 'YOUR_PROCESSOR_ID', // Create this in the Cloud Console
  gcsInputUri = 'gs://cloud-samples-data/documentai/invoice.pdf',
  gcsOutputUri = 'output-bucket',
  gcsOutputUriPrefix = uuid.v4()
)

Source from the content-addressed store, hash-verified

18const uuid = require('uuid');
19
20async function main(
21 projectId = 'YOUR_PROJECT_ID',
22 location = 'YOUR_PROJECT_LOCATION',
23 processorId = 'YOUR_PROCESSOR_ID', // Create this in the Cloud Console
24 gcsInputUri = 'gs://cloud-samples-data/documentai/invoice.pdf',
25 gcsOutputUri = 'output-bucket',
26 gcsOutputUriPrefix = uuid.v4()
27) {
28 // [START documentai_batch_process_document]
29 /**
30 * TODO(developer): Uncomment these variables before running the sample.
31 */
32 // const projectId = 'YOUR_PROJECT_ID';
33 // const location = 'YOUR_PROJECT_LOCATION'; // Format is 'us' or 'eu'
34 // const processorId = 'YOUR_PROCESSOR_ID';
35 // const gcsInputUri = 'YOUR_SOURCE_PDF';
36 // const gcsOutputUri = 'YOUR_STORAGE_BUCKET';
37 // const gcsOutputUriPrefix = 'YOUR_STORAGE_PREFIX';
38
39 // Imports the Google Cloud client library
40 const {DocumentProcessorServiceClient} =
41 require('@google-cloud/documentai').v1;
42 const {Storage} = require('@google-cloud/storage');
43
44 // Instantiates Document AI, Storage clients
45 const client = new DocumentProcessorServiceClient();
46 const storage = new Storage();
47
48 const {default: PQueue} = require('p-queue');
49
50 async function batchProcessDocument() {
51 const name = `projects/${projectId}/locations/${location}/processors/${processorId}`;
52
53 // Configure the batch process request.
54 const request = {
55 name,
56 inputDocuments: {
57 gcsDocuments: {
58 documents: [
59 {
60 gcsUri: gcsInputUri,
61 mimeType: 'application/pdf',
62 },
63 ],
64 },
65 },
66 documentOutputConfig: {
67 gcsOutputConfig: {
68 gcsUri: `${gcsOutputUri}/${gcsOutputUriPrefix}/`,
69 },
70 },
71 };
72
73 // Batch process document using a long-running operation.
74 // You can wait for now, or get results later.
75 // Note: first request to the service takes longer than subsequent
76 // requests.
77 const [operation] = await client.batchProcessDocuments(request);

Callers 1

Calls 1

batchProcessDocumentFunction · 0.85

Tested by

no test coverage detected