获取处理器类型 (v1)

获取特定 Document AI 处理器类型的详细信息。

代码示例

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/option"
)

// getProcessorType gets details of a Document AI processor type.
func getProcessorType(ctx context.Context, w io.Writer, projectID string, locationID string, processorTypeID string) error {
	name := fmt.Sprintf("projects/%s/locations/%s/processorTypes/%s", projectID, locationID, processorTypeID)

	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.GetProcessorTypeRequest{
		Name: name,
	}

	resp, err := client.GetProcessorType(ctx, req)
	if err != nil {
		return fmt.Errorf("GetProcessorType failed: %w", err)
	}

	fmt.Fprintf(w, "Processor type name: %s\n", resp.GetName())
	fmt.Fprintf(w, "Type: %s\n", resp.GetType())
	fmt.Fprintf(w, "Category: %s\n", resp.GetCategory())
	fmt.Fprintf(w, "Allow creation: %t\n", resp.GetAllowCreation())

	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.GetProcessorTypeRequest;
import com.google.cloud.documentai.v1.ProcessorType;
import com.google.cloud.documentai.v1.ProcessorTypeName;

public class GetProcessorType {

  public static void main(String[] args) throws Exception {
    String projectId = "example-project-id";
    String locationId = "example-location-id";
    String processorTypeId = "example-processor-type-id";
    getProcessorType(projectId, locationId, processorTypeId);
  }

  public static void getProcessorType(String projectId, String locationId, String processorTypeId)
      throws Exception {
    // Configure regional endpoint for Document AI
    String endpoint = String.format("%s-documentai.googleapis.com:443", locationId);
    DocumentProcessorServiceSettings settings =
        DocumentProcessorServiceSettings.newBuilder().setEndpoint(endpoint).build();

    try (DocumentProcessorServiceClient client = DocumentProcessorServiceClient.create(settings)) {
      String name = ProcessorTypeName.of(projectId, locationId, processorTypeId).toString();

      GetProcessorTypeRequest request =
          GetProcessorTypeRequest.newBuilder().setName(name).build();

      ProcessorType processorType = client.getProcessorType(request);
      System.out.println("Processor Type: " + processorType.getType());
      System.out.println("Category: " + processorType.getCategory());
      System.out.println("Resource Name: " + processorType.getName());
    }
  }
}

Node.js

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

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

'use strict';

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

const client = new DocumentProcessorServiceClient();

/**
 * Use the getProcessorType method
 *
 * @param {string} projectId - The Google Cloud project ID.
 * @param {string} locationId - The location ID where processor types are hosted (e.g., 'us' or 'eu').
 * @param {string} processorTypeId - The processor type identifier (e.g., 'OCR_PROCESSOR', 'INVOICE_PROCESSOR').
 */
async function getProcessorType(projectId, locationId, processorTypeId) {
  try {
    const name = `projects/${projectId}/locations/${locationId}/processorTypes/${processorTypeId}`;
    const request = {
      name,
    };

    const [processorType] = await client.getProcessorType(request);

    console.log(`Processor Type: ${processorType.type}`);
    console.log(`Category: ${processorType.category}`);
    console.log(`Available: ${processorType.allowCreation ? 'Yes' : 'No'}`);
    return processorType;
  } catch (err) {
    if (err.code === status.NOT_FOUND) {
      console.error(`Processor type not found: ${err.message}`);
    } else if (err.code === status.PERMISSION_DENIED) {
      console.error(`Permission denied: ${err.message}`);
    } else {
      console.error(`API error: ${err.message}`);
    }
  }
}

module.exports = { getProcessorType };

后续步骤

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