啟用及停用服務
您可以使用「服務使用情形」為 Google Cloud 專案啟用或停用任何可用的服務。包括公開的Google Cloud 服務,以及使用 Cloud Endpoints 建立的私人服務。如要進一步瞭解公開與私人服務之間的差異,請參閱公開與私人服務。
大多數使用者都能透過 Google Cloud 控制台啟用及停用服務。如要編寫指令碼,請使用 Google Cloud CLI。如要透過程式輔助方式存取 Service Usage API,建議使用用戶端程式庫。如要試用 API,您可以傳送要求,不必設定完整的開發環境。
如要瞭解服務使用情形配額限制,請參閱「配額與限制」。您可能會想要限制付費用量。視 API 而定,您可以明確限制要求數量。
事前準備
啟用及停用服務前,您必須先為「服務使用情形」設定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 角色,只要您已獲授角色,即可選取任何專案。
-
建立專案:如要建立專案,您需要具備專案建立者角色 (
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,您需要具備服務使用情形管理員 IAM 角色 (
roles/serviceusage.serviceUsageAdmin),其中包含serviceusage.services.enable權限。瞭解如何授予角色。gcloud services enable serviceusage.googleapis.com
-
安裝 Google Cloud CLI。
-
若您採用的是外部識別資訊提供者 (IdP),請先使用聯合身分登入 gcloud CLI。
-
執行下列指令,初始化 gcloud CLI:
gcloud init -
選取或建立專案所需的角色
- 選取專案:選取專案時,不需要具備特定 IAM 角色,只要您已獲授角色,即可選取任何專案。
-
建立專案:如要建立專案,您需要具備專案建立者角色 (
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,您需要具備服務使用情形管理員 IAM 角色 (
roles/serviceusage.serviceUsageAdmin),其中包含serviceusage.services.enable權限。瞭解如何授予角色。gcloud services enable serviceusage.googleapis.com
必要的角色
如要取得啟用及停用服務所需的權限,請要求管理員授予您專案的服務使用情形管理員 (roles/serviceusage.serviceUsageAdmin) IAM 角色。如要進一步瞭解如何授予角色,請參閱「管理專案、資料夾和組織的存取權」。
可啟用服務
啟用服務的程序可能有所不同。部分 API 會要求您先接受服務條款,或處於預覽階段,需要先獲得核准才能使用。其他 API 可能會要求額外資訊,例如您機構的聯絡資訊,以及您應用程式的名稱。Google 會對您提供的所有資訊善盡保密責任。我們會藉此瞭解 API 使用情形,並在必要時與您聯絡。
控制台
前往 Google Cloud 控制台的「APIs & Services」(API 與服務) >「API Library」(API 程式庫) 頁面。
選取近期專案,或使用控制台工具列的資源選取器,選取要啟用 API 的 Google Cloud 專案。
按一下要啟用的 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。
如果停用 Google Kubernetes Engine API,專案中正在執行的叢集就會暫停。30 天後,系統就會刪除這些已暫停的叢集。如要重新啟用叢集,請重新啟用 GKE API。叢集最多可能需要 24 小時才會恢復。詳情請參閱「叢集生命週期」。
控制台
前往 Google Cloud 控制台的「APIs & Services」(API 與服務) >「API Library」(API 程式庫) 頁面。
選取近期專案,或使用控制台工具列的資源選取器,選取要停用 API 的 Google Cloud 專案。
按一下要停用的 API,或使用「Search for APIs & Services」(搜尋 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。
您會在原始要求的相關回覆中收到這個 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 時,即代表作業完成。