使用客户端库处理文档
本页面介绍了如何以您偏好的编程语言开始使用 Document AI API。
准备工作
- 登录您的 Google Cloud 账号。如果您是新手 Google Cloud, 请创建一个账号来评估我们的产品在 实际场景中的表现。新客户还可获享 $300 赠金,用于 运行、测试和部署工作负载。
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
Enable the Document AI API.
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. Learn how to grant roles.-
Create a service account:
-
Ensure that you have the Create Service Accounts IAM role
(
roles/iam.serviceAccountCreator) and the Project IAM Admin role (roles/resourcemanager.projectIamAdmin). Learn how to grant roles. -
In the Google Cloud console, go to the Create service account page.
Go to Create service account - Select your project.
-
In the Service account name field, enter a name. The Google Cloud console fills in the Service account ID field based on this name.
In the Service account description field, enter a description. For example,
Service account for quickstart. - Click Create and continue.
-
Grant the Document AI > Document AI Administrator role to the service account.
To grant the role, find the Select a role list, then select Document AI > Document AI Administrator.
- Click Continue.
-
Click Done to finish creating the service account.
Do not close your browser window. You will use it in the next step.
-
Ensure that you have the Create Service Accounts IAM role
(
-
Create a service account key:
- In the Google Cloud console, click the email address for the service account that you created.
- Click Keys.
- Click Add key, and then click Create new key.
- Click Create. A JSON key file is downloaded to your computer.
- Click Close.
-
Set the environment variable
GOOGLE_APPLICATION_CREDENTIALSto the path of the JSON file that contains your credentials. This variable applies only to your current shell session, so if you open a new session, set the variable again. -
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
Enable the Document AI API.
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. Learn how to grant roles.-
Create a service account:
-
Ensure that you have the Create Service Accounts IAM role
(
roles/iam.serviceAccountCreator) and the Project IAM Admin role (roles/resourcemanager.projectIamAdmin). Learn how to grant roles. -
In the Google Cloud console, go to the Create service account page.
Go to Create service account - Select your project.
-
In the Service account name field, enter a name. The Google Cloud console fills in the Service account ID field based on this name.
In the Service account description field, enter a description. For example,
Service account for quickstart. - Click Create and continue.
-
Grant the Document AI > Document AI Administrator role to the service account.
To grant the role, find the Select a role list, then select Document AI > Document AI Administrator.
- Click Continue.
-
Click Done to finish creating the service account.
Do not close your browser window. You will use it in the next step.
-
Ensure that you have the Create Service Accounts IAM role
(
-
Create a service account key:
- In the Google Cloud console, click the email address for the service account that you created.
- Click Keys.
- Click Add key, and then click Create new key.
- Click Create. A JSON key file is downloaded to your computer.
- Click Close.
-
Set the environment variable
GOOGLE_APPLICATION_CREDENTIALSto the path of the JSON file that contains your credentials. This variable applies only to your current shell session, so if you open a new session, set the variable again.
安装客户端库
C#
如需详细了解如何设置 C# 开发环境,请参阅 C# 开发环境设置指南。
Install-Package Google.Cloud.DocumentAI.V1 -Pre
Go
go get cloud.google.com/go/documentai
Java
如需详细了解如何设置 Java 开发环境,请参阅Java 开发环境设置指南。
如果您使用的是 Maven,请将以下代码添加到您的 pom.xml 文件中。如需详细了解
BOM,请参阅Google Cloud Platform 库 BOM。
如果您使用的是 Gradle, 请将以下代码添加到您的依赖项中:
如果您使用的是 sbt,请将 以下代码添加到您的依赖项中:
如果您使用的是 Visual Studio Code 或 IntelliJ,可以通过以下 IDE 插件将客户端库添加到您的 项目中:
上述插件还提供其他功能,例如服务账号密钥管理。如需了解详情,请参阅各个插件相应的文档。
Node.js
如需详细了解如何设置 Node.js 开发环境,请参阅Node.js 开发环境设置指南。
npm install @google-cloud/documentai
PHP
composer require google/cloud-document-ai
Python
如需详细了解如何设置 Python 开发环境,请参阅 Python 开发环境设置指南。
pip install --upgrade google-cloud-documentai
Ruby
如需详细了解如何设置 Ruby 开发环境,请参阅Ruby 开发环境设置指南。
gem install google-cloud-document_ai
文件处理
使用 Document AI API 从本地 PDF 文档请求信息。如需运行以下示例,您必须先在界面中创建处理器。
控制台
创建处理器并获得处理器 ID 后,运行以下代码以请求单独的文件处理:
C#
如需了解详情,请参阅 Document AI C# API 参考文档。
如需向 Document AI 进行身份验证,请设置应用默认凭据。如需了解详情,请参阅 为本地开发环境设置身份验证。
C++
如需了解详情,请参阅 Document AI C++ API 参考文档。
如需向 Document AI 进行身份验证,请设置应用默认凭据。如需了解详情,请参阅 为本地开发环境设置身份验证。
Go
如需了解详情,请参阅 Document AI Go API 参考文档。
如需向 Document AI 进行身份验证,请设置应用默认凭据。如需了解详情,请参阅 为本地开发环境设置身份验证。
Java
如需了解详情,请参阅 Document AI Java API 参考文档。
如需向 Document AI 进行身份验证,请设置应用默认凭据。如需了解详情,请参阅 为本地开发环境设置身份验证。
Node.js
如需了解详情,请参阅 Document AI Node.js API 参考文档。
如需向 Document AI 进行身份验证,请设置应用默认凭据。如需了解详情,请参阅 为本地开发环境设置身份验证。
PHP
如需了解详情,请参阅 Document AI PHP API 参考文档。
如需向 Document AI 进行身份验证,请设置应用默认凭据。如需了解详情,请参阅 为本地开发环境设置身份验证。
Python
如需了解详情,请参阅 Document AI Python API 参考文档。
如需向 Document AI 进行身份验证,请设置应用默认凭据。如需了解详情,请参阅 为本地开发环境设置身份验证。
Ruby
如需了解详情,请参阅 Document AI Ruby API 参考文档。
如需向 Document AI 进行身份验证,请设置应用默认凭据。如需了解详情,请参阅 为本地开发环境设置身份验证。
结果怎么样?
清理
为避免系统因本快速入门中使用的资源向您的 Google 账号收取费用,请按照以下步骤操作:
- 使用 Google Cloud console 删除您不需要的项目。
后续步骤
详细了解我们的 Document AI API 客户端库。