本頁說明如何開始使用 Compute Engine API 適用的 Cloud 用戶端程式庫。用戶端程式庫可讓您從支援的語言輕鬆存取Google Cloud API。雖然您可以直接向伺服器發出原始要求來使用Google Cloud API,但用戶端程式庫提供簡化功能,可大幅減少您需要編寫的程式碼數量。
如要進一步瞭解 Cloud 用戶端程式庫和舊版 Google API 用戶端程式庫,請參閱用戶端程式庫說明。
如要直接在 Google Cloud 控制台按照逐步指南操作,請按一下「Guide me」(逐步引導):
安裝用戶端程式庫
C++
請按照
Quickstart
操作。
C#
從 NuGet 安裝 Google.Cloud.Compute.V1
套件。
詳情請參閱設定 C# 開發環境。
Go
go get cloud.google.com/go/compute/apiv1
詳情請參閱「設定 Go 開發環境」。
Java
If you are using Maven, add
the following to your pom.xml
file. For more information about
BOMs, see The Google Cloud Platform Libraries BOM.
If you are using Gradle, add the following to your dependencies:
If you are using sbt, add the following to your dependencies:
舊版的 Compute Engine 專用 Java 版 Cloud Client Libraries 可在 Maven 構件中取得,版本為 0.120.x 或更舊。這個程式庫的 0.120.x 以下版本與後續版本不相容。
詳情請參閱「設定 Java 開發環境」。
Node.js
npm install @google-cloud/compute
舊版 Compute Engine 適用的 Node.js Cloud 用戶端程式庫,可在 npm 套件中以 2.5.x 以下版本提供。這個程式庫的 2.5.x 版和更早版本與後續版本不相容。
詳情請參閱「設定 Node.js 開發環境」。
PHP
composer require google/cloud-compute
詳情請參閱「在 Google Cloud 上使用 PHP」。
Python
pip install --upgrade google-cloud-compute
詳情請參閱「設定 Python 開發環境」。
Ruby
gem install google-cloud-compute-v1
詳情請參閱「設定 Ruby 開發環境」。
設定驗證方法
為驗證對 Google Cloud API 的呼叫,用戶端程式庫支援應用程式預設憑證 (ADC);程式庫會在定義的一組位置中尋找憑證,並使用這些憑證驗證對 API 的要求。使用 ADC,您可以在各種環境 (例如本機開發或正式版) 中,為應用程式提供憑證,不必修改應用程式程式碼。在實際工作環境中,設定 ADC 的方式取決於服務和環境。詳情請參閱「設定應用程式預設憑證」。
在本地開發環境中,您可以使用與 Google 帳戶相關聯的憑證設定 ADC:
-
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.
-
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.
畫面上會顯示登入畫面。登入後,您的憑證會儲存在 ADC 使用的 本機憑證檔案中。
使用用戶端程式庫
以下範例說明如何使用用戶端程式庫,列出特定區域中的執行個體。如需更多範例,請參閱「使用用戶端程式庫」。
C#
Go
Java
Node.js
PHP
Python
Ruby
其他資源
C++
以下清單列出與 C++ 用戶端程式庫相關的更多資源連結:
C#
下列清單包含 C# 專用用戶端程式庫的相關資源連結:
Go
下列清單包含與 Go 專用用戶端程式庫相關的更多資源連結:
Java
以下列出與 Java 用戶端程式庫相關的更多資源連結:
Node.js
以下清單列出與 Node.js 用戶端程式庫相關的更多資源連結:
PHP
下列清單包含與 PHP 用戶端程式庫相關的更多資源連結:
Python
以下清單包含適用於 Python 的用戶端程式庫相關資源連結:
Ruby
以下清單提供與 Ruby 用戶端程式庫相關的更多資源連結:
舊版用戶端程式庫
Cloud 用戶端程式庫採用最新的用戶端程式庫模型,建議您使用這個選項,以程式輔助的方式存取 Cloud API。
如果無法使用 Cloud 用戶端程式庫,可以改用下列 Google API 用戶端程式庫:
語言 | 程式庫 | 資源 |
---|---|---|
Go | Google API Go 用戶端程式庫 | 說明文件 |
Java | Google API Java 用戶端程式庫 | 說明文件 |
JavaScript | Google API JavaScript 用戶端程式庫 | 說明文件 |
.NET | Google API .NET 用戶端程式庫 | 說明文件 |
Node.js | Google API Node.js 用戶端程式庫 | 說明文件 |
Objective-C | Google API Objective-C 用戶端程式庫 | 說明文件 |
PHP | Google API PHP 用戶端程式庫 | 說明文件 |
Python | Google API Python 用戶端程式庫 | 說明文件 |
Ruby | Google API Ruby 用戶端程式庫 | 說明文件 |
Dart | Google API Dart 用戶端程式庫 | 說明文件 |
第三方 Compute Engine API 用戶端程式庫
libcloud
libcloud 是一個 Python 程式庫,可透過單一整合式 API 與多個雲端服務供應商互動。
自 2013 年 7 月起,Apache libcloud API 專案已開始支援 Compute Engine 並提供相關更新。支援多種 Compute Engine 功能,包括執行個體、磁碟、網路和負載平衡器。入門版示範提供程式碼範例,說明如何搭配使用 libcloud 和 Compute Engine。
jclouds
jclouds 是一個開放原始碼程式庫,可讓您透過多個 Cloud 供應商使用 Java 和 Clojure。
jclouds 雲端 API 支援 Compute Engine,可讓您管理虛擬機器、磁碟和網路等資源。自 1.9 版起,Compute Engine 已升級為 jclouds 核心。
fog.io
fog.io 是一個開放原始碼 Ruby 程式庫,可讓您透過單一 API 與多種雲端服務互動。
自 2013 年 5 月的 1.11.0 版起,fog.io cloud API 已開始支援 Compute Engine。可支援執行個體作業 (例如建立和刪除),以及其他資源的管理作業,例如磁碟、網路和負載平衡器。