設定預設處理器版本 (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 範例瀏覽工具。