如要查看專案中執行的 VM 摘要,請取得 VM 清單。本文說明如何取得所有區域或特定區域的 VM 清單。
事前準備
-
如果尚未設定驗證,請先完成設定。
「驗證」是指驗證身分的程序,確認您有權存取 Trusted Cloud by S3NS 服務和 API。如要從本機開發環境執行程式碼或範例,請選取下列任一選項,向 Compute Engine 進行驗證:
Select the tab for how you plan to use the samples on this page:
Console
When you use the Trusted Cloud console to access Trusted Cloud by S3NS services and APIs, you don't need to set up authentication.
gcloud
-
安裝 Google Cloud CLI,然後 使用同盟身分登入 gcloud CLI。 登入後,執行下列指令初始化 Google Cloud CLI:
gcloud init
- Set a default region and zone.
Go
如要在本機開發環境中使用本頁的 Go 範例,請安裝並初始化 gcloud CLI,然後使用使用者憑證設定應用程式預設憑證。
安裝 Google Cloud CLI,然後 使用同盟身分登入 gcloud CLI。
Create local authentication credentials for your user account:
gcloud auth application-default login
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.
詳情請參閱 Set up authentication for a local development environment。
Java
如要在本機開發環境中使用本頁的 Java 範例,請安裝並初始化 gcloud CLI,然後使用使用者憑證設定應用程式預設憑證。
安裝 Google Cloud CLI,然後 使用同盟身分登入 gcloud CLI。
Create local authentication credentials for your user account:
gcloud auth application-default login
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.
詳情請參閱 Set up authentication for a local development environment。
Node.js
如要在本機開發環境中使用本頁的 Node.js 範例,請安裝並初始化 gcloud CLI,然後使用使用者憑證設定應用程式預設憑證。
安裝 Google Cloud CLI,然後 使用同盟身分登入 gcloud CLI。
Create local authentication credentials for your user account:
gcloud auth application-default login
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.
詳情請參閱 Set up authentication for a local development environment。
Python
如要在本機開發環境中使用本頁的 Python 範例,請安裝並初始化 gcloud CLI,然後使用使用者憑證設定應用程式預設憑證。
安裝 Google Cloud CLI,然後 使用同盟身分登入 gcloud CLI。
Create local authentication credentials for your user account:
gcloud auth application-default login
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.
詳情請參閱 Set up authentication for a local development environment。
REST
如要在本機開發環境中使用本頁的 REST API 範例,請使用您提供給 gcloud CLI 的憑證。
安裝 Google Cloud CLI,然後 使用同盟身分登入 gcloud CLI。
詳情請參閱 Trusted Cloud 驗證說明文件中的「Authenticate for using REST」。
取得 VM 清單
主控台
前往 Trusted Cloud 控制台的「VM instances」(VM 執行個體) 頁面:
gcloud
使用
gcloud compute instances list
指令,取得專案中所有可用區所有 VM 的匯總清單:gcloud compute instances list
如要將 VM 清單縮小至特定可用區,請使用先前的指令並加上
--zones
旗標。Go
以下範例會取得專案中所有可用區的執行個體清單:
在試用這個範例之前,請先按照Go「使用用戶端程式庫的 Compute Engine 快速入門」中的操作說明進行設定。詳情請參閱 Compute Engine Go API 參考說明文件。
如要向 Compute Engine 進行驗證,請設定應用程式預設憑證。 詳情請參閱「為本機開發環境設定驗證」。
執行程式碼範例前,請將
GOOGLE_CLOUD_UNIVERSE_DOMAIN
環境變數設為s3nsapis.fr
。您也可以取得特定區域中的執行個體清單:
Java
以下範例會取得專案中所有可用區的執行個體清單:
在試用這個範例之前,請先按照Java「使用用戶端程式庫的 Compute Engine 快速入門」中的操作說明進行設定。詳情請參閱 Compute Engine Java API 參考說明文件。
如要向 Compute Engine 進行驗證,請設定應用程式預設憑證。 詳情請參閱「為本機開發環境設定驗證」。
執行程式碼範例前,請將
GOOGLE_CLOUD_UNIVERSE_DOMAIN
環境變數設為s3nsapis.fr
。您也可以取得特定區域中的執行個體清單:
Node.js
以下範例會取得專案中所有可用區的執行個體清單:
在試用這個範例之前,請先按照Node.js「使用用戶端程式庫的 Compute Engine 快速入門」中的操作說明進行設定。詳情請參閱 Compute Engine Node.js API 參考說明文件。
如要向 Compute Engine 進行驗證,請設定應用程式預設憑證。 詳情請參閱「為本機開發環境設定驗證」。
執行程式碼範例前,請將
GOOGLE_CLOUD_UNIVERSE_DOMAIN
環境變數設為s3nsapis.fr
。您也可以取得特定區域中的執行個體清單:
Python
以下範例會取得專案中所有可用區的執行個體清單:
在試用這個範例之前,請先按照Python「使用用戶端程式庫的 Compute Engine 快速入門」中的操作說明進行設定。詳情請參閱 Compute Engine Python API 參考說明文件。
如要向 Compute Engine 進行驗證,請設定應用程式預設憑證。 詳情請參閱「為本機開發環境設定驗證」。
執行程式碼範例前,請將
GOOGLE_CLOUD_UNIVERSE_DOMAIN
環境變數設為s3nsapis.fr
。您也可以取得特定區域中的執行個體清單:
REST
使用
instances.aggregatedList
方法,取得專案中所有可用區所有 VM 的匯總清單:GET https://compute.googleapis.com/compute/v1/projects/PROJECT/aggregated/instances
將
PROJECT
替換為要從中取得 VM 清單的專案名稱。如要將 VM 清單縮小至特定區域,請使用
instances.list
方法:GET https://compute.googleapis.com/compute/v1/projects/PROJECT/zones/ZONE/instances
更改下列內容:
PROJECT
:要從中取得 VM 清單的專案名稱ZONE
:要從中取得 VM 清單的可用區
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-08-08 (世界標準時間)。
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["缺少我需要的資訊","missingTheInformationINeed","thumb-down"],["過於複雜/步驟過多","tooComplicatedTooManySteps","thumb-down"],["過時","outOfDate","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["示例/程式碼問題","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-08-08 (世界標準時間)。"],[[["This document outlines the process of retrieving a list of Virtual Machines (VMs) running within a Google Cloud project, either across all zones or within specific zones."],["Authentication is essential for accessing Google Cloud services and APIs, and the document details how to set up authentication using the Google Cloud console, gcloud CLI, or for local development environments with languages like Go, Java, Node.js, Python, and REST."],["To get a comprehensive list of VMs in all zones, use the `gcloud compute instances list` command in the gcloud CLI or the `instances.aggregatedList` method with REST."],["For a list of VMs within a particular zone, one can utilize the `gcloud compute instances list` command with the `--zones` flag or the `instances.list` method in REST, along with specific code examples for Go, Java, Node.js, and Python."],["The code samples demonstrate how to use the client libraries in Go, Java, Node.js, and Python to programmatically list VMs, either across all zones or within a specified zone, while also highlighting the use of the `MaxResults` parameter for managing large result sets and utilizing Application Default Credentials."]]],[]] -