快速入门:设置 Document AI API

本指南介绍了开始使用 Document AI 所需的所有设置步骤。

Google Cloud 控制台简介

Google Cloud console 是一个网页界面,用于预配、配置、管理和监控使用 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.

Go to project selector

启用 API

您必须为项目启用 Document AI API。 如需详细了解如何启用 API,请参阅 Service Usage 文档

Enable the Document AI API.

Enable the API

启用结算功能

结算账号定义了一组给定资源的付款方。 结算账号可以关联到一个或多个项目。 项目的资源使用费将记在关联的结算账号名下。您应在创建项目时配置结算信息。 如需了解详情,请参阅结算文档

Make sure that billing is enabled for your Google Cloud project.

位置

借助 Document AI,您可以控制存储和处理项目资源的位置。具体而言,在创建处理器时,您必须选择一个位置来存储和处理数据。默认情况下,Document AI 会在美国位置存储和处理资源。如果您选择欧盟位置,您的数据和流程将仅存储在欧盟。

使用 API 设置位置

每次使用 API 发送处理请求时,您都必须指定处理器的位置。例如,如果您的处理器配置为在欧盟地区存储和处理数据,请按如下方式使用 URI eu-documentai.googleapis.com

Process
  • https://eu-documentai.googleapis.com/v1/projects/$PROJECT_ID/locations/eu/processors/$PROCESSOR_ID:process
  • https://eu-documentai.googleapis.com/v1beta3/projects/$PROJECT_ID/locations/eu/processors/$PROCESSOR_ID:process
batchProcess
  • https://eu-documentai.googleapis.com/v1/projects/$PROJECT_ID/locations/eu/processors/$PROCESSOR_ID:batchProcess
  • https://eu-documentai.googleapis.com/v1beta3/projects/$PROJECT_ID/locations/eu/processors/$PROCESSOR_ID:batchProcess

安装 Document AI API 客户端库

您可以通过以下三种方式调用 Document AI API:

客户端库支持多种主流语言。如需了解如何安装客户端库,请参阅 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

    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,然后使用您的用户凭证设置应用默认凭证。

    1. Install the Google Cloud CLI.

    2. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

    3. To initialize the gcloud CLI, run the following command:

      gcloud init
    4. 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.

    Google Cloud

    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 身份验证文档中的使用 REST 时进行身份验证

    如需了解如何为生产环境设置身份验证,请参阅 Google Cloud 身份验证文档中的 Set up Application Default Credentials for code running on Google Cloud

    角色简介

    在调用 API 时, Google Cloud 要求调用方身份(任何适用的用户、实体或进程及其指定的属性)拥有适当的权限。 您可以通过为用户账号或服务账号授予角色来提供权限。 如需了解详情,请参阅 Identity and Access Management (IAM) 文档

    如需试用 Document AI API,您可以在以下步骤中使用项目 > Owner 角色。 项目 > Owner 角色会为服务账号授予对项目的完整访问权限。 如果您的请求不需要完整权限,您可以使用 Google Cloud 控制台指定更具限制性的角色。如需查看 Document AI 的权限和角色列表,请参阅 Document AI 权限Document AI 角色。 如需了解如何使用 IAM 角色管理权限,请参阅管理对项目、文件夹和组织的访问权限

    跨项目文件访问设置

    在一个项目中设置 Document AI 处理器后,您可能希望该项目能够访问存储在同一组织中(托管 Document AI 处理器)的其他项目中的输入文件。

    如需允许跨项目访问,您必须向默认 Document AI 服务账号授予 Storage Object Viewer 角色 (roles/storage.objectViewer),如下图所示。

    setup-1

    示例

    • 假设项目 A 托管 Document AI 处理器,并且可以选择性地托管一个用于写入存储桶处理器输出的存储桶。
    • 项目 B 拥有包含 Document AI 处理器输入文件的存储桶。
    • 如需让项目 A 能够访问项目 B 中的文件,您必须向项目 A 的 Document AI 服务账号授予项目 B 中输入存储桶的 Storage Object Viewer 角色 (roles/storage.objectViewer)。

      setup-2

    如需详细了解 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

    setup-3

    后续步骤:使用场景

    启用 Document AI API 后,即可创建和使用 Document AI 处理器。哪种类型的处理器最好取决于您的使用场景。