將 Identity and Access Management (IAM) 角色授予資源使用者前,您可能想知道有哪些角色可授予特定資源。
事前準備
Enable the IAM API.
設定驗證方法。
Select the tab for how you plan to use the samples on this page:
Console
When you use the Google Cloud console to access Google Cloud services and APIs, you don't need to set up authentication.
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.
-
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.
-
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.
-
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.
-
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.
前往 Google Cloud 控制台的「IAM」頁面。
按一下頁面頂端的「選取專案」。
選取要查看可授權角色的專案、資料夾或機構。
按一下「授予存取權」
。按一下「選擇角色」。這個選單會顯示您可針對這項資源授予的所有角色,包括自訂角色。
前往 Google Cloud 控制台,找出您要查看可授予角色的資源,並前往該資源的頁面。
舉例來說,如要管理 Compute Engine 執行個體的存取權,請前往「VM instances」(VM 執行個體) 頁面。
找出要查看可授予角色的資源,然後勾選旁邊的核取方塊。
確認資訊面板顯示在畫面上。如果沒有顯示,請按一下「顯示資訊面板」。
按一下「新增主體」
。按一下「選擇角色」。這個選單會顯示您可針對這項資源授予的所有角色,包括自訂角色。
FULL_RESOURCE_NAME
:由服務名稱和資源路徑組成的 URI。如需範例,請參閱「完整資源名稱」。- 閱讀可用的 IAM 角色說明。
- 瞭解如何選擇最合適的預先定義角色。
- 瞭解如何授予、變更及撤銷主體的存取權。
- 如需不同類型資源的資源名稱範例,請參閱這篇文章。
In the Google Cloud console, activate Cloud Shell.
At the bottom of the Google Cloud console, a Cloud Shell session starts and displays a command-line prompt. Cloud Shell is a shell environment with the Google Cloud CLI already installed and with values already set for your current project. It can take a few seconds for the session to initialize.
C#
如要在本機開發環境中使用本頁的 .NET 範例,請安裝並初始化 gcloud CLI,然後使用使用者憑證設定應用程式預設憑證。
詳情請參閱 Google Cloud 驗證說明文件中的「 為本機開發環境設定 ADC」。
C++
如要在本機開發環境中使用本頁的 C++ 範例,請安裝並初始化 gcloud CLI,然後使用使用者憑證設定應用程式預設憑證。
詳情請參閱 Google Cloud 驗證說明文件中的「 為本機開發環境設定 ADC」。
Go
如要在本機開發環境中使用本頁的 Go 範例,請安裝並初始化 gcloud CLI,然後使用使用者憑證設定應用程式預設憑證。
詳情請參閱 Google Cloud 驗證說明文件中的「 為本機開發環境設定 ADC」。
Java
如要在本機開發環境中使用本頁的 Java 範例,請安裝並初始化 gcloud CLI,然後使用使用者憑證設定應用程式預設憑證。
詳情請參閱 Google Cloud 驗證說明文件中的「 為本機開發環境設定 ADC」。
Python
如要在本機開發環境中使用本頁的 Python 範例,請安裝並初始化 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」。
必要的角色
如要取得列出可授予角色的權限,請要求管理員為您授予要列出可授予角色的資源的安全性審查者 (
這個角色具備所有資源類型的roles/iam.securityReviewer
) IAM 角色。如要進一步瞭解如何授予角色,請參閱「管理專案、資料夾和機構的存取權」。getIamPolicy
權限。如要列出可授予的角色,您必須具備要列出可授予角色資源的getIamPolicy
權限。舉例來說,如要列出專案可授予的角色,您必須具備resourcemanager.projects.getIamPolicy
權限。瞭解可以授予哪些角色
如果角色包含了某個資源類型的任何權限,就能針對這類資源或其上層授予這個角色。舉例來說,
storage.admin
角色授予storage.buckets.get
和storage.objects.get
API 的權限,因此可以針對「Storage Buckets」和「Storage Objects」資源類型授予這個角色。此外,角色也能在其權限定義適用的資源類型「上層」授予。換句話說,您可以在 Google Cloud 資源階層中較高的資源上,授予較低層級資源的角色。以
storage.admin
角色為例,除了「Storage Buckets」外,您也能在專案或機構層級授予這個角色。角色授予的權限只會影響指定層級或較低層級的資源,不會影響較高層級或同一層級的資源。此外,針對資源授予角色後,系統只會授予適用於指定資源的權限,而不論角色的名稱、說明或其中含有的其他權限。舉例來說,在專案層級將
resourcemanager.organizationAdmin
角色 (授予resourcemanager.projects.list
權限) 指派給使用者,只會授予該使用者特定專案的權限。而不會允許他們列出或管理機構中的所有專案。 同理可證,對特定 Compute Engine 執行個體指派compute.admin
角色,只會授予適用於這個執行個體的權限,而不會包含專案中的其他項目。列出可授予的角色
您可以使用 Google Cloud 控制台、Google Cloud CLI、IAM API 或 IAM 用戶端程式庫,列出可授予的角色。
Google Cloud 主控台一律會列出您檢視資源的所有可授權角色。Google Cloud CLI、IAM API 和用戶端程式庫只會列出已啟用 API 的可授權角色。
控制台
如要查看專案、資料夾或機構可授予的角色,請按照下列步驟操作:
如要查看其他資源類型可授予的角色,請按照下列步驟操作:
gcloud
您可以使用
gcloud iam list-grantable-roles
指令,傳回可套用到特定資源的所有角色清單。gcloud iam list-grantable-roles full-resource-name
視需要的資源,系統可能會傳回大量的角色。如要限制結果,可以指定篩選運算式。
輸出結果如下所示:
description: Full control of all Compute Engine resources. name: roles/compute.admin title: Compute Admin --- description: Full control of Compute Engine instance resources. name: roles/compute.instanceAdmin title: Compute Instance Admin # Additional results here...
C++
如要瞭解如何安裝及使用 IAM 的用戶端程式庫,請參閱 IAM 用戶端程式庫。 詳情請參閱 IAM C++ API 參考說明文件。
如要向 IAM 進行驗證,請設定應用程式預設憑證。 詳情請參閱「事前準備」。
C#
如要瞭解如何安裝及使用 IAM 的用戶端程式庫,請參閱 IAM 用戶端程式庫。 詳情請參閱 IAM C# API 參考說明文件。
如要向 IAM 進行驗證,請設定應用程式預設憑證。 詳情請參閱「事前準備」。
Go
如要瞭解如何安裝及使用 IAM 的用戶端程式庫,請參閱 IAM 用戶端程式庫。 詳情請參閱 IAM Go API 參考說明文件。
如要向 IAM 進行驗證,請設定應用程式預設憑證。 詳情請參閱「事前準備」。
Java
如要瞭解如何安裝及使用 IAM 的用戶端程式庫,請參閱 IAM 用戶端程式庫。 詳情請參閱 IAM Java API 參考說明文件。
如要向 IAM 進行驗證,請設定應用程式預設憑證。 詳情請參閱「事前準備」。
Python
如要瞭解如何安裝及使用 IAM 的用戶端程式庫,請參閱 IAM 用戶端程式庫。 詳情請參閱 IAM Python API 參考說明文件。
如要向 IAM 進行驗證,請設定應用程式預設憑證。 詳情請參閱「事前準備」。
REST
roles.queryGrantableRoles
方法會傳回可授予資源的所有角色清單。使用任何要求資料之前,請先替換以下項目:
HTTP 方法和網址:
POST https://iam.googleapis.com/v1/roles:queryGrantableRoles
JSON 要求主體:
{ "fullResourceName": "FULL_RESOURCE_NAME" }
如要傳送要求,請展開以下其中一個選項:
您應該會收到如下的 JSON 回應:
{ "roles": [ { "name": "roles/compute.admin", "title": "Compute Admin", "description": "Full control of all Compute Engine resources." }, { "name": "roles/compute.instanceAdmin", "title": "Compute Instance Admin (beta)", "description": "Full control of Compute Engine instance resources." } ] }
在以上的範例中,「完整資源名稱」是一種無配置的 URI,由 DNS 相容的 API 服務名稱和資源路徑所組成。
舉例來說,如要傳回可針對專案授予的所有角色,請使用:
//cloudresourcemanager.googleapis.com/projects/project-id
較低層級的資源擁有較詳細的完整名稱。例如使用以下指令,傳回可針對 Compute Engine 執行個體授予的所有角色:
//compute.googleapis.com/projects/project-id/zones/zone-name/instances/instance-id
後續步驟
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-07-14 (世界標準時間)。
-