Listar processadores (v1)

Lista todos os processadores em um projeto e local especificados.

Exemplo de código

Go

Para mais informações, consulte a documentação de referência da API Document AI Go.

Para autenticar na Document AI, configure o Application Default Credentials. Para mais informações, consulte Configurar a autenticação para um ambiente de desenvolvimento local.


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

Para mais informações, consulte a documentação de referência da API Document AI Node.js.

Para autenticar na Document AI, configure o Application Default Credentials. Para mais informações, consulte Configurar a autenticação para um ambiente de desenvolvimento local.

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

A seguir

Para pesquisar e filtrar exemplos de código de outros Google Cloud produtos, consulte a Google Cloud pesquisa de exemplos de código.