本指南提供開始使用 Document AI 的所有必要設定步驟。
關於 Google Cloud 控制台
Google Cloud console 是一個網頁版 UI,可用於佈建、設定、管理和監控使用 Google Cloud 產品的系統。您可以使用 Google Cloud 控制台設定及管理 Document AI 資源。
建立專案
如要使用 Google Cloud提供的服務,您必須建立專案,專案會整理所有 Google Cloud 資源,並包含下列元件:
- 一組協作者
- 已啟用的 API (和其他資源)
- 監控工具
- 帳單資訊
- 驗證和存取權控管
- Document AI 處理器
您可以建立一個或多個專案。您可以使用專案,在資源階層中整理 Google Cloud 資源。如要進一步瞭解專案,請參閱 Resource Manager 說明文件。
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
啟用 API
您必須為專案啟用 Document AI API。如要進一步瞭解如何啟用 API,請參閱服務使用情形說明文件。
Enable the Document AI API.
啟用計費功能
帳單帳戶會定義特定資源組合的費用由誰支付。帳單帳戶可連結至一或多個專案。專案的使用費會從相連結的帳單帳戶中扣除。您會在建立專案時設定帳單。詳情請參閱帳單說明文件。
Make sure that billing is enabled for your Google Cloud project.
位置
您可以透過 Document AI,控管專案資源的儲存和處理位置。具體來說,建立處理器時,您必須選擇資料的儲存和處理位置。根據預設,Document AI 會在美國境內儲存及處理資源。如果選擇歐盟位置,資料和程序只會儲存在歐盟。
使用 API 設定位置資訊
使用 API 傳送處理要求時,請務必指定處理器的位置。舉例來說,如果處理器設定為在歐盟境內儲存及處理資料,請使用 URI eu-documentai.googleapis.com
,如下所示:
Process |
|
batchProcess |
|
安裝 Document AI API 用戶端程式庫
您可以透過三種方式呼叫 Document AI API:
- Google 支援的用戶端程式庫 (建議)
- REST
- gRPC
我們提供多種常見語言的用戶端程式庫。如要瞭解如何安裝用戶端程式庫,請參閱 Document AI API 用戶端程式庫。
安裝並初始化 Google Cloud CLI
gcloud CLI 提供一套工具,可用來管理託管於 Google Cloud的資源和應用程式。
以下連結提供操作說明:
After installing the Google Cloud CLI, initialize it by running the following command:
gcloud init
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
設定驗證方法
任何使用該 API 的用戶端應用程式都必須經過驗證,並獲得所要求資源的存取權。驗證設定方式取決於您是在本機開發環境中作業,還是設定實際工作環境。詳情請參閱「設定應用程式預設憑證」。
Select the tabs for how you plan to access the API:
gcloud
-
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
If you're using a local shell, then create local authentication credentials for your user account:
gcloud auth application-default login
You don't need to do this if you're using Cloud Shell.
If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.
- 假設 專案 A 裝載 Document AI 處理器,並視需要裝載處理器輸出內容寫入的 bucket。
- 專案 B 擁有包含 Document AI 處理器輸入檔案的 bucket。
如要讓專案 A 存取專案 B 中的檔案,您必須將專案 B 中輸入 bucket 的「Storage 物件檢視者」角色 (
roles/storage.objectViewer
) 授予專案 A 的 Document AI 服務帳戶。
After installing the Google Cloud CLI, initialize it by running the following command:
gcloud init
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
用戶端程式庫
如要在本機開發環境中使用用戶端程式庫,請安裝並初始化 gcloud CLI,然後使用您的使用者憑證設定應用程式預設憑證。
詳情請參閱 Google Cloud 驗證說明文件中的「 為本機開發環境設定 ADC」。
REST
如要在本機開發環境中使用 REST API,請使用您提供給 gcloud CLI 的憑證。
After installing the Google Cloud CLI, initialize it by running the following command:
gcloud init
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
詳情請參閱 Google Cloud 驗證說明文件中的「Authenticate for using REST」。
如要瞭解如何為正式版環境設定驗證,請參閱 Google Cloud 驗證說明文件中的 Set up Application Default Credentials for code running on Google Cloud。
關於角色
呼叫 API 時, Google Cloud 會要求呼叫身分 (任何適用的人員、實體或程序及其定義的屬性) 具備適當的權限。您可以將角色授予使用者帳戶或服務帳戶,藉此授予權限。詳情請參閱身分與存取權管理 (IAM) 說明文件。
為了試用 Document AI API,您可以在下列步驟中使用「專案 > 擁有者」角色。「專案 > 擁有者」角色會授予服務帳戶專案中資源的完整權限。如果您的要求不需要完整權限,可以使用Google Cloud 控制台指定限制較多的角色。如需 Document AI 的權限和角色清單,請參閱「Document AI 權限」和「Document AI 角色」。如要瞭解如何使用 IAM 角色管理權限,請參閱「管理專案、資料夾和機構的存取權」。
設定跨專案檔案存取權
在一個專案中設定 Document AI 處理器時,您可能希望這個專案存取儲存在相同機構中不同專案的輸入檔案,而這些專案會代管 Document AI 處理器。
如要允許跨專案存取,您必須將 Storage 物件檢視者角色 (roles/storage.objectViewer
) 授予預設的 Document AI 服務帳戶,如下圖所示。
範例
如要進一步瞭解 IAM 和 Storage Object Viewer
,請參閱
Cloud Storage 的 IAM 角色。
內建服務帳戶
Document AI 服務帳戶的命名慣例如下:
service-{project number}@gcp-sa-prod-dai-core.iam.gserviceaccount.com
範例:service-361747088407@gcp-sa-prod-dai-core.iam.gserviceaccount.com
後續步驟:用途
啟用 Document AI API 後,即可建立及使用 Document AI 處理器。最佳處理器類型取決於您的用途。
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-07-14 (世界標準時間)。