启用和停用服务
您可以使用 Service Usage 为 Google Cloud 项目启用或停用任何可用服务。这包括公共Google Cloud 服务和使用 Cloud Endpoints 创建的专用服务。如需详细了解公共服务和专用服务之间的差异,请参阅公共服务和专用服务。
大多数用户都可以使用 Google Cloud 控制台启用和停用服务。对于脚本,请使用 Google Cloud CLI。如需以编程方式访问 Service Usage API,建议使用客户端库。如需试用该 API,您可以发送请求,而无需设置完整的开发环境。
如需了解服务使用情况配额限制,请参阅配额和限制。您可能希望限制计费使用量。根据 API,您可以明确限制请求。
准备工作
在启用和停用服务之前,您必须为 Service Usage 设置Google Cloud 项目和环境。
控制台
- 登录您的 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.
-
If you're using an existing project for this guide, verify that you have the permissions required to complete this guide. If you created a new project, then you already have the required permissions.
-
Verify that billing is enabled for your Google Cloud project.
Enable the Service Usage 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.-
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.
-
If you're using an existing project for this guide, verify that you have the permissions required to complete this guide. If you created a new project, then you already have the required permissions.
-
Verify that billing is enabled for your Google Cloud project.
Enable the Service Usage 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.
gcloud
- 登录您的 Google Cloud 账号。如果您是 Google Cloud新手,请 创建一个账号来评估我们的产品在实际场景中的表现。新客户还可获享 $300 赠金,用于运行、测试和部署工作负载。
-
安装 Google Cloud CLI。
-
如果您使用的是外部身份提供方 (IdP),则必须先使用联合身份登录 gcloud CLI。
-
如需初始化 gcloud CLI,请运行以下命令:
gcloud init -
选择或创建项目所需的角色
- 选择项目:选择项目不需要特定的 IAM 角色,您可以选择已获授角色的任何项目。
-
创建项目:如需创建项目,您需要拥有 Project Creator 角色 (
roles/resourcemanager.projectCreator),该角色包含resourcemanager.projects.create权限。了解如何授予角色。
-
创建 Google Cloud 项目:
gcloud projects create PROJECT_ID
将
PROJECT_ID替换为您要创建的 Google Cloud 项目的名称。 -
选择您创建的 Google Cloud 项目:
gcloud config set project PROJECT_ID
将
PROJECT_ID替换为您的 Google Cloud 项目名称。
-
如果您要使用现有项目来完成本指南,请验证您是否拥有完成本指南所需的权限。如果您创建了新项目,则您已拥有所需的权限。
启用 Service Usage API:
启用 API 所需的角色
如需启用 API,您需要拥有 Service Usage Admin IAM 角色 (
roles/serviceusage.serviceUsageAdmin),该角色包含serviceusage.services.enable权限。了解如何授予角色。gcloud services enable serviceusage.googleapis.com
-
安装 Google Cloud CLI。
-
如果您使用的是外部身份提供方 (IdP),则必须先使用联合身份登录 gcloud CLI。
-
如需初始化 gcloud CLI,请运行以下命令:
gcloud init -
选择或创建项目所需的角色
- 选择项目:选择项目不需要特定的 IAM 角色,您可以选择已获授角色的任何项目。
-
创建项目:如需创建项目,您需要拥有 Project Creator 角色 (
roles/resourcemanager.projectCreator),该角色包含resourcemanager.projects.create权限。了解如何授予角色。
-
创建 Google Cloud 项目:
gcloud projects create PROJECT_ID
将
PROJECT_ID替换为您要创建的 Google Cloud 项目的名称。 -
选择您创建的 Google Cloud 项目:
gcloud config set project PROJECT_ID
将
PROJECT_ID替换为您的 Google Cloud 项目名称。
-
如果您要使用现有项目来完成本指南,请验证您是否拥有完成本指南所需的权限。如果您创建了新项目,则您已拥有所需的权限。
启用 Service Usage API:
启用 API 所需的角色
如需启用 API,您需要拥有 Service Usage Admin IAM 角色 (
roles/serviceusage.serviceUsageAdmin),该角色包含serviceusage.services.enable权限。了解如何授予角色。gcloud services enable serviceusage.googleapis.com
所需的角色
如需获得启用和停用服务所需的权限,请让您的管理员为您授予项目的 Service Usage Admin (roles/serviceusage.serviceUsageAdmin) IAM 角色。如需详细了解如何授予角色,请参阅管理对项目、文件夹和组织的访问权限。
启用服务
启用服务的过程可能因服务而异。有些 API 会要求您先接受其服务条款,或者处于预览状态,需要获得批准才能使用。其他 API 可能会要求您提供更多信息,例如您组织的联系信息以及您应用的名称。所有请求的信息都会在 Google 内部保密。我们会使用此电子邮件地址来了解 API 使用情况,并在必要时与您联系。
控制台
在 Google Cloud 控制台中,前往 API 和服务 > API 库页面。
选择近期的项目,或使用控制台工具栏上的资源选择器选择要启用 API 的 Google Cloud 项目。
点击要启用的 API,或使用搜索 API 和服务框搜索该 API。
点击启用。
gcloud
gcloud services enable 命令会为当前项目启用指定名称的服务。
在使用下面的命令数据之前,请先进行以下替换:
-
SERVICE:您要启用的服务的名称。
执行 gcloud services enable 命令:
Linux、macOS 或 Cloud Shell
gcloud services enable SERVICE
Windows (PowerShell)
gcloud services enable SERVICE
Windows (cmd.exe)
gcloud services enable SERVICE
您应该会收到类似如下所示的响应:
Waiting for async operation operations/OPERATION_ID to complete...
Operation finished successfully.
如需启用多项服务,请使用以空格分隔的服务名称。例如,以下命令会同时启用 BigQuery 和 Pub/Sub 服务:
gcloud services enable bigquery.googleapis.com pubsub.googleapis.com
如需检索操作详细信息,请使用 gcloud services operations describe 命令:
gcloud services operations describe operations/OPERATION_ID
将 OPERATION_ID 替换为在原始请求的响应中收到的操作 ID。
REST API
启用单项服务
如需启用单项服务,请使用 services.enable 方法。
在使用任何请求数据之前,请先进行以下替换:
PROJECT_ID:您的 Google Cloud项目 ID。SERVICE:您要启用的服务的名称。
如需发送您的请求,请展开以下选项之一:
如果成功,响应正文将包含一个新创建的 Operation 实例:
{
"name": "operations/OPERATION_ID"
}
启用多项服务
如需在单次操作中启用最多 20 项服务,请使用 services.batchEnable 方法。
在使用任何请求数据之前,请先进行以下替换:
PROJECT_ID:您的 Google Cloud项目 ID。SERVICE1:您要启用的服务的名称。SERVICE2:您要启用的另一项服务的名称。
请求 JSON 正文:
{
"serviceIds": ["SERVICE1", "SERVICE2"]
}
如需发送您的请求,请展开以下选项之一:
如果成功,响应正文将包含一个新创建的 Operation 实例:
{
"name": "operations/OPERATION_ID",
"metadata": {
"@type": "type.googleapis.com/google.api.serviceusage.v1.OperationMetadata"
}
}
停用服务
您可以随时为项目停用 API。不过,您仍需支付待处理费用以及项目中使用的所有计费 API 的费用。如需了解详情,请参阅 Cloud API 的使用入门指南。
许多服务(例如 Cloud Storage 和 BigQuery)都会收取数据存储费用。停用服务的 API 不会删除您的数据,您仍需继续支付相应费用。如需避免日后产生费用并停止使用某项服务,请先删除所有关联的数据,然后再停用其 API。
如果您停用 Kubernetes Engine API,项目中正在运行的集群会暂停。30 天后,这些暂停的集群会被删除。如需重新激活集群,请重新启用 GKE API。集群最长可能需要 24 小时才能恢复。如需了解详情,请参阅集群生命周期。
控制台
在 Google Cloud 控制台中,前往 API 和服务 > API 库页面。
选择近期的项目,或使用控制台工具栏上的资源选择器选择要停用 API 的 Google Cloud 项目。
点击要禁用的 API,或使用搜索 API 和服务框搜索该 API。
点击 停用 API。
gcloud
gcloud services disable 命令会针对当前项目停用指定名称的服务。
在使用下面的命令数据之前,请先进行以下替换:
-
SERVICE:要停用的服务的名称。
执行 gcloud services disable 命令:
Linux、macOS 或 Cloud Shell
gcloud services disable SERVICE
Windows (PowerShell)
gcloud services disable SERVICE
Windows (cmd.exe)
gcloud services disable SERVICE
您应该会收到类似如下所示的响应:
Waiting for async operation operations/OPERATION_ID to complete...
Operation finished successfully.
如需停用多项服务,请使用以空格分隔的服务名称。例如,以下命令会同时停用 BigQuery 和 Pub/Sub 服务:
gcloud services disable bigquery.googleapis.com pubsub.googleapis.com
如需检索操作详细信息,请使用 gcloud services operations describe 命令:
gcloud services operations describe operations/OPERATION_ID
将 OPERATION_ID 替换为在原始请求的响应中收到的操作 ID。
REST API
如需停用服务,请使用 services.disable 方法。
在使用任何请求数据之前,请先进行以下替换:
PROJECT_ID:您的 Google Cloud项目 ID。SERVICE:要停用的服务的名称。
如需发送您的请求,请展开以下选项之一:
如果成功,响应正文将包含一个新创建的 Operation 实例:
{
"name": "operations/OPERATION_ID"
}
检查长时间运行的操作的状态
使用启用或停用方法会返回长时间运行的操作 (LRO)。LRO 会跟踪请求的状态,并指示服务更改是否已完成。
在使用任何请求数据之前,请先进行以下替换:
OPERATION_ID:操作标识符。
您将在原始请求的响应中收到此 ID。
如需发送您的请求,请展开以下选项之一:
您应该收到类似以下内容的 JSON 响应:
{
"name": "operations/OPERATION_ID",
"metadata": {
"@type": "type.googleapis.com/google.protobuf.Empty"
},
"done": true,
"response": {
"@type": "type.googleapis.com/google.api.serviceusage.v1.EnableServiceResponse",
"service": {
"name": "projects/PROJECT_NUMBER/services/SERVICE",
"config": {
"name": "SERVICE",
"title": "TITLE",
"documentation": {},
"quota": {},
"authentication": {},
"usage": {
"requirements": [
"serviceusage.googleapis.com/tos/cloud"
]
},
"monitoring": {}
},
"state": "ENABLED",
"parent": "projects/PROJECT_NUMBER"
}
}
}
当 done 字段设置为 true 时,操作完成。