Crea processore (v1)

Crea un nuovo processore Document AI.

Esempio di codice

Node.js

Per saperne di più, consulta la documentazione di riferimento dell'API Document AI Node.js.

Per eseguire l'autenticazione in Document AI, configura le Credenziali predefinite dell'applicazione. Per saperne di più, consulta Configura l'autenticazione per un ambiente di sviluppo locale.

'use strict';

const {DocumentProcessorServiceClient} = require('@google-cloud/documentai');
const {status} = require('@grpc/grpc-js');

const client = new DocumentProcessorServiceClient();

/**
 * Creates a new Document AI processor.
 *
 * @param {string} projectId - The Google Cloud project ID (for example, 'example-project-id')
 * @param {string} locationId - The Google Cloud region where the service is located (for example, 'us-central1')
 */
async function createProcessor(projectId, locationId) {
  try {
    const parent = client.locationPath(projectId, locationId);
    const processor = {
      displayName: 'Form Processor',
      type: 'FORM_PARSER_PROCESSOR',
    };

    const request = {
      parent,
      processor,
    };

    const [response] = await client.createProcessor(request);
    console.log(`Processor created: ${response.name}`);
    console.log(`Display name: ${response.displayName}`);
    console.log(`Type: ${response.type}`);
  } catch (err) {
    if (err.code === status.ALREADY_EXISTS) {
      console.error('Processor already exists in the specified location.');
    } else if (err.code === status.INVALID_ARGUMENT) {
      console.error('Invalid processor configuration provided.');
    } else {
      console.error(`Failed to create processor: ${err.message}`);
    }
  }
}

module.exports = { createProcessor };

Python

Per saperne di più, consulta la documentazione di riferimento dell'API Document AI Python.

Per eseguire l'autenticazione in Document AI, configura le Credenziali predefinite dell'applicazione. Per saperne di più, consulta Configura l'autenticazione per un ambiente di sviluppo locale.

import google.api_core.exceptions
from google.cloud import documentai_v1

client = documentai_v1.DocumentProcessorServiceClient()


def create_processor(project_id: str, location_id: str) -> None:
    """Creates a Document AI processor.

    Args:
        project_id: The Google Cloud project ID (for example, 'example-project-id').
        location_id: The Google Cloud location or region where the processor is created (for example, 'us' or 'eu').
    """
    parent = f"projects/{project_id}/locations/{location_id}"
    processor = documentai_v1.Processor(
        display_name="Example OCR Processor",
        type_="OCR_PROCESSOR",
    )

    try:
        response = client.create_processor(parent=parent, processor=processor)
        print(f"Created processor: {response.name}")
        print(f"Display name: {response.display_name}")
        print(f"Type: {response.type_}")
    except google.api_core.exceptions.GoogleAPICallError as error:
        print(f"Failed to create processor: {error}")

Passaggi successivi

Per cercare e filtrare gli esempi di codice per altri prodotti Google Cloud , consulta il browser degli esempi diGoogle Cloud .