This page shows how to get started with the Cloud Client Libraries for the Pub/Sub API. Read more about the client libraries for Cloud APIs in Client Libraries Explained.
Installing the client library
C++
For more information about installing the C++ library,
see the GitHub README
.
C#
For more information, see Setting Up a C# Development Environment.
Install-Package Google.Cloud.PubSub.V1 -Pre
Go
For more information, see Setting Up a Go Development Environment.
go get -u cloud.google.com/go/pubsub
Java
For more information, see Setting Up a Java Development Environment.
Maven を使用している場合は、次のものを pom.xml
ファイルに追加します。BOM の詳細については、Google Cloud Platform ライブラリ BOM をご覧ください。
Gradle を使用している場合は、次のものを依存関係に追加します。
sbt を使用している場合は、次のものを依存関係に追加します。
IntelliJ または Eclipse を使用している場合は、次の IDE プラグインを使用してプロジェクトにクライアント ライブラリを追加できます。
プラグインでは、サービス アカウントのキー管理などの追加機能も提供されます。詳細は各プラグインのドキュメントをご覧ください。
Node.js
For more information, see Setting Up a Node.js Development Environment.
npm install --save @google-cloud/pubsub
PHP
For more information, see Using PHP on Google Cloud.
composer require google/cloud-pubsub
Python
For more information, see Setting Up a Python Development Environment.
pip install --upgrade google-cloud-pubsub
Ruby
For more information, see Setting Up a Ruby Development Environment.
gem install google-cloud-pubsub
Setting up authentication
To run the client library, you must first set up authentication by creating a service account and setting an environment variable. Complete the following steps to set up authentication. For other ways to authenticate, see the GCP authentication documentation.
Cloud Console
サービス アカウントを作成します。
-
Cloud Console で [サービス アカウントの作成] ページに移動します。
[サービス アカウントの作成] に移動 - プロジェクトを選択します。
-
[サービス アカウント名] フィールドに名前を入力します。 Cloud Console は、この名前に基づいて [サービス アカウント ID] フィールドに入力します。
[サービス アカウントの説明] フィールドに説明を入力します。例:
Service account for quickstart
。 - [作成] をクリックします。
-
[ロールを選択] フィールドをクリックします。
[クイック アクセス] で [基本] をクリックし、[オーナー] をクリックします。
- [次へ] をクリックします。
-
[完了] をクリックして、サービス アカウントの作成を完了します。
ブラウザ ウィンドウを閉じないでください。次のステップでこれを使用します。
サービス アカウント キーを作成します。
- Cloud Console で、作成したサービス アカウントのメールアドレスをクリックします。
- [キー] をクリックします。
- [キーを追加] をクリックし、[新しいキーを作成] をクリックします。
- [CREATE] をクリックします。JSON キーファイルがパソコンにダウンロードされます。
- [閉じる] をクリックします。
コマンドライン
ローカルマシン上の Cloud SDK を使用するか、または Cloud Shell 内で以下のコマンドを実行できます。
-
サービス アカウントを作成します。NAME をサービス アカウントの名前に置き換えます。
gcloud iam service-accounts create NAME
-
サービス アカウントに権限を付与します。PROJECT_ID を実際のプロジェクト ID に置き換えます。
gcloud projects add-iam-policy-binding PROJECT_ID --member="serviceAccount:NAME@PROJECT_ID.iam.gserviceaccount.com" --role="roles/owner"
-
キーファイルを生成します。FILE_NAME はキーファイルの名前に置き換えてください。
gcloud iam service-accounts keys create FILE_NAME.json --iam-account=NAME@PROJECT_ID.iam.gserviceaccount.com
環境変数 GOOGLE_APPLICATION_CREDENTIALS
を設定して、アプリケーション コードに認証情報を指定します。[PATH] は、サービス アカウント キーが含まれる JSON ファイルのファイルパスに置き換えます。この変数は現在のシェル セッションにのみ適用されるため、新しいセッションを開く場合は、変数を再度設定します。
Linux または macOS
export GOOGLE_APPLICATION_CREDENTIALS="[PATH]"
例:
export GOOGLE_APPLICATION_CREDENTIALS="/home/user/Downloads/my-key.json"
Windows
PowerShell を使用する場合:
$env:GOOGLE_APPLICATION_CREDENTIALS="[PATH]"
例:
$env:GOOGLE_APPLICATION_CREDENTIALS="C:\Users\username\Downloads\my-key.json"
コマンド プロンプトを使用する場合:
set GOOGLE_APPLICATION_CREDENTIALS=[PATH]
Using the client library
The following example shows how to use the client library.
C++
Before trying this sample, follow the C++ setup instructions in the Pub/Sub Quickstart Using Client Libraries. For more information, see the Pub/Sub C++ API reference documentation.
C#
Before trying this sample, follow the C# setup instructions in the Pub/Sub Quickstart Using Client Libraries. For more information, see the Pub/Sub C# API reference documentation.
Go
Before trying this sample, follow the Go setup instructions in the Pub/Sub Quickstart Using Client Libraries. For more information, see the Pub/Sub Go API reference documentation.
If you're using the first generation of the App Engine standard runtime, we recommend using the older Google APIs Client Libraries. See this GitHub issue for more details.Java
Before trying this sample, follow the Java setup instructions in the Pub/Sub Quickstart Using Client Libraries. For more information, see the Pub/Sub Java API reference documentation.
Node.js
Before trying this sample, follow the Node.js setup instructions in the Pub/Sub Quickstart Using Client Libraries. For more information, see the Pub/Sub Node.js API reference documentation.
PHP
Before trying this sample, follow the PHP setup instructions in the Pub/Sub Quickstart Using Client Libraries. For more information, see the Pub/Sub PHP API reference documentation.
Python
Before trying this sample, follow the Python setup instructions in the Pub/Sub Quickstart Using Client Libraries. For more information, see the Pub/Sub Python API reference documentation.
Ruby
Before trying this sample, follow the Ruby setup instructions in the Pub/Sub Quickstart Using Client Libraries. For more information, see the Pub/Sub Ruby API reference documentation.