设置默认处理器版本 (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"
)

// Use the SetDefaultProcessorVersion method.
func setDefaultProcessorVersion(ctx context.Context, w io.Writer, projectID string, locationID string, processorID string, processorVersionID string) error {
	processor := fmt.Sprintf("projects/%s/locations/%s/processors/%s", projectID, locationID, processorID)
	defaultProcessorVersion := fmt.Sprintf("projects/%s/locations/%s/processors/%s/processorVersions/%s", projectID, locationID, processorID, processorVersionID)

	client, err := documentai.NewDocumentProcessorClient(ctx)
	if err != nil {
		return fmt.Errorf("NewDocumentProcessorClient failed: %w", err)
	}
	defer client.Close()

	req := &documentaipb.SetDefaultProcessorVersionRequest{
		Processor:               processor,
		DefaultProcessorVersion: defaultProcessorVersion,
	}

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

	// SetDefaultProcessorVersion is a long-running operation; wait for completion.
	if _, err := op.Wait(ctx); err != nil {
		return fmt.Errorf("Wait: %w", err)
	}

	fmt.Fprintf(w, "Set default processor version to %s\n", defaultProcessorVersion)

	return nil
}

后续步骤

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