프로세서 만들기(v1)

새 Document AI 프로세서를 만듭니다.

코드 샘플

Node.js

자세한 내용은 Document AI Node.js API 참고 문서를 참조하세요.

Document AI에 인증하려면 애플리케이션 기본 사용자 인증 정보를 설정합니다. 자세한 내용은 로컬 개발 환경의 인증 설정을 참조하세요.

'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

자세한 내용은 Document AI Python API 참고 문서를 참조하세요.

Document AI에 인증하려면 애플리케이션 기본 사용자 인증 정보를 설정합니다. 자세한 내용은 로컬 개발 환경의 인증 설정을 참조하세요.

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}")

다음 단계

다른 Google Cloud 제품의 코드 샘플을 검색하고 필터링하려면 Google Cloud 샘플 브라우저 참조하기