프로세서 버전 나열(v1)

프로세서에 사용 가능한 버전을 나열합니다.

코드 샘플

Go

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

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


import (
	"context"
	"fmt"
	"io"

	documentai "cloud.google.com/go/documentai/apiv1beta3"
	"cloud.google.com/go/documentai/apiv1beta3/documentaipb"
	"google.golang.org/api/iterator"
	"google.golang.org/api/option"
)

// listProcessorVersions lists the versions available for a processor.
func listProcessorVersions(ctx context.Context, w io.Writer, projectID string, locationID string, processorID string) error {
	parent := fmt.Sprintf("projects/%s/locations/%s/processors/%s", projectID, locationID, processorID)

	endpoint := fmt.Sprintf("%s-documentai.googleapis.com:443", locationID)
	client, err := documentai.NewDocumentProcessorClient(ctx, option.WithEndpoint(endpoint))
	if err != nil {
		return fmt.Errorf("NewDocumentProcessorClient failed: %w", err)
	}
	defer client.Close()

	req := &documentaipb.ListProcessorVersionsRequest{
		Parent: parent,
	}

	it := client.ListProcessorVersions(ctx, req)
	for {
		resp, err := it.Next()
		if err == iterator.Done {
			break
		}
		if err != nil {
			return fmt.Errorf("failed to iterate processor versions: %w", err)
		}

		fmt.Fprintf(w, "Processor version: %s\n", resp.GetName())
		fmt.Fprintf(w, "Display name: %s\n", resp.GetDisplayName())
		fmt.Fprintf(w, "State: %s\n", resp.GetState())
	}

	return nil
}

Java

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

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

import com.google.cloud.documentai.v1.DocumentProcessorServiceClient;
import com.google.cloud.documentai.v1.DocumentProcessorServiceSettings;
import com.google.cloud.documentai.v1.ProcessorVersion;

public class ListProcessorVersions {

  public static void main(String[] args) throws Exception {
    String projectId = "example-project_id";
    String locationId = "example-location_id";
    String processorId = "example-processor_id";
    listProcessorVersions(projectId, locationId, processorId);
  }

  public static void listProcessorVersions(
      String projectId, String locationId, String processorId) throws Exception {
    // Co-locate client requests with the location where the processor resides.
    String endpoint = String.format("%s-documentai.googleapis.com:443", locationId);
    DocumentProcessorServiceSettings settings =
        DocumentProcessorServiceSettings.newBuilder().setEndpoint(endpoint).build();

    try (DocumentProcessorServiceClient client = DocumentProcessorServiceClient.create(settings)) {
      String parent =
          String.format(
              "projects/%s/locations/%s/processors/%s", projectId, locationId, processorId);

      for (ProcessorVersion processorVersion : client.listProcessorVersions(parent).iterateAll()) {
        System.out.printf("Processor version name: %s%n", processorVersion.getName());
        System.out.printf("Display name: %s%n", processorVersion.getDisplayName());
        System.out.printf("State: %s%n", processorVersion.getState());
      }
    }
  }
}

Node.js

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

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

'use strict';

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

/**
 * Lists processor versions for a given 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')
 * @param {string} processorId - ID of the Processor (for example, 'example-processor-id')
 */
async function listProcessorVersions(projectId, locationId, processorId) {
  // Instantiates a client configured for the specific regional endpoint
  const client = new DocumentProcessorServiceClient({
    apiEndpoint: `${locationId}-documentai.googleapis.com`,
  });

  try {
    const parent = client.processorPath(projectId, locationId, processorId);

    const request = {
      parent,
    };

    const [processorVersions] = await client.listProcessorVersions(request);

    console.log(`Found ${processorVersions.length} processor version(s).`);
    for (const version of processorVersions) {
      console.log(`Name: ${version.name}`);
      console.log(`Display Name: ${version.displayName}`);
      console.log(`State: ${version.state}`);
    }
  } catch (err) {
    if (err.code === status.NOT_FOUND) {
      console.error('The specified processor or location was not found.');
    } else if (err.code === status.INVALID_ARGUMENT) {
      console.error('Invalid argument provided for processor versions request.');
    } else if (err.code === status.PERMISSION_DENIED) {
      console.error('Permission denied for the requested resource.');
    } else {
      console.error('An API error occurred:', err.message);
    }
  }
}

module.exports = { listProcessorVersions };

다음 단계

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