Membuat daftar pemroses (v1)

Mencantumkan semua pemroses dalam project dan lokasi yang ditentukan.

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

// listProcessors lists all processors in the specified project and location.
func listProcessors(ctx context.Context, w io.Writer, projectID string, locationID string) error {
	parent := fmt.Sprintf("projects/%s/locations/%s", projectID, locationID)

	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.ListProcessorsRequest{
		Parent: parent,
	}

	it := client.ListProcessors(ctx, req)
	for {
		processor, err := it.Next()
		if errors.Is(err, iterator.Done) {
			break
		}
		if err != nil {
			return fmt.Errorf("failed to list processors: %w", err)
		}

		fmt.Fprintf(w, "Processor Name: %s\n", processor.GetName())
		fmt.Fprintf(w, "Display Name: %s\n", processor.GetDisplayName())
		fmt.Fprintf(w, "Type: %s\n", processor.GetType())
		fmt.Fprintf(w, "State: %s\n", processor.GetState())
	}

	return nil
}

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 Document AI processors in a given project and location.
 *
 * @param {string} projectId - The Google Cloud project ID (for example, 'example-project-id')
 * @param {string} locationId - The Google Cloud location (for example, 'us-central1')
 */
async function listProcessors(projectId, locationId) {
  const client = new DocumentProcessorServiceClient({
    apiEndpoint: `${locationId}-documentai.googleapis.com`,
  });

  try {
    const parent = client.locationPath(projectId, locationId);
    const request = {
      parent,
    };

    const [processors] = await client.listProcessors(request);

    if (processors.length === 0) {
      console.log('No processors found.');
      return;
    }

    for (const processor of processors) {
      console.log(`Processor Name: ${processor.name}`);
      console.log(`Processor Type: ${processor.type}`);
      console.log(`Display Name: ${processor.displayName}`);
    }
  } catch (err) {
    if (err.code === status.INVALID_ARGUMENT) {
      console.error('Invalid argument provided for listing processors:', err.message);
    } else if (err.code === status.NOT_FOUND) {
      console.error('Specified location or project was not found:', err.message);
    } else if (err.code === status.PERMISSION_DENIED) {
      console.error('Insufficient permissions to list processors:', err.message);
    } else {
      console.error('An error occurred while listing processors:', err);
    }
  }
}

module.exports = { listProcessors };

Langkah berikutnya

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