Membuat daftar versi pemroses (v1)

Membuat daftar versi yang tersedia untuk pemroses.

Contoh kode

Go

Untuk mengetahui informasi selengkapnya, lihat dokumentasi referensi API Go Document AI.

Untuk melakukan autentikasi ke Document AI, siapkan Kredensial Default Aplikasi. Untuk mengetahui informasi selengkapnya, lihat Menyiapkan autentikasi untuk lingkungan pengembangan lokal.


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

Untuk mengetahui informasi selengkapnya, lihat dokumentasi referensi API Java Document AI.

Untuk melakukan autentikasi ke Document AI, siapkan Kredensial Default Aplikasi. Untuk mengetahui informasi selengkapnya, lihat Menyiapkan autentikasi untuk lingkungan pengembangan lokal.

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

Untuk mengetahui informasi selengkapnya, lihat dokumentasi referensi API Node.js Document AI.

Untuk melakukan autentikasi ke Document AI, siapkan Kredensial Default Aplikasi. Untuk mengetahui informasi selengkapnya, lihat Menyiapkan autentikasi untuk lingkungan pengembangan lokal.

'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 };

Langkah berikutnya

Untuk menelusuri dan memfilter contoh kode untuk produk Google Cloud lainnya, lihat browser contohGoogle Cloud .