列出处理器 (v1)

列出指定项目和位置中的所有处理器。

代码示例

Go

如需了解详情,请参阅 Document AI Go API 参考文档。

如需向 Document AI 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅为本地开发环境设置身份验证。


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

如需了解详情,请参阅 Document AI Node.js API 参考文档。

如需向 Document AI 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅为本地开发环境设置身份验证。

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

后续步骤

如需搜索和过滤其他 Google Cloud 产品的代码示例,请参阅Google Cloud 示例浏览器。