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.
Wenn Sie Maven verwenden, fügen Sie Ihrer Datei pom.xml
den folgenden Code hinzu. Weitere Informationen zu BOMs finden Sie unter The Google Cloud Platform Libraries BOM.
Wenn Sie Gradle verwenden, fügen Sie den Abhängigkeiten Folgendes hinzu:
Wenn Sie sbt nutzen, fügen Sie den Abhängigkeiten Folgendes hinzu:
Bei Verwendung von IntelliJ oder Eclipse können Sie Ihrem Projekt mithilfe der folgenden IDE-Plug-ins Clientbibliotheken hinzufügen:
Diese Plug-ins bieten zusätzliche Funktionen wie die Schlüsselverwaltung für Dienstkonten. Einzelheiten finden Sie in der Dokumentation der einzelnen Plug-ins.
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
-
Wechseln Sie in der Cloud Console zur Seite Dienstkontoschlüssel erstellen.
Zur Seite „Dienstkontoschlüssel erstellen“ - Wählen Sie aus der Liste Dienstkonto die Option Neues Dienstkonto aus.
- Geben Sie im Feld Dienstkontoname einen Namen ein.
Wählen Sie in der Liste Rolle die Option Projekt > Inhaber
- Klicken Sie auf Erstellen. Eine JSON-Datei mit Ihrem Schlüssel wird auf Ihren Computer heruntergeladen.
Befehlszeile
Sie können die folgenden Befehle mithilfe des Cloud SDK auf Ihrem lokalen Computer oder in Cloud Shell ausführen.
-
Erstellen Sie das Dienstkonto. Ersetzen Sie NAME mit einem Namen für das Dienstkonto.
gcloud iam service-accounts create NAME
-
Gewähren Sie dem Dienstkonto Berechtigungen. Geben Sie für PROJECT_ID Ihre Projekt-ID an.
gcloud projects add-iam-policy-binding PROJECT_ID --member="serviceAccount:NAME@PROJECT_ID.iam.gserviceaccount.com" --role="roles/owner"
-
Erstellen Sie die Schlüsseldatei. Geben Sie für FILE_NAME einen Namen für die Schlüsseldatei an.
gcloud iam service-accounts keys create FILE_NAME.json --iam-account=NAME@PROJECT_ID.iam.gserviceaccount.com
Die Anmeldedaten zur Authentifizierung für Ihren Anwendungscode geben Sie durch Festlegung der Umgebungsvariable GOOGLE_APPLICATION_CREDENTIALS
an.
Geben Sie für [PATH] den Dateipfad der JSON-Datei an, die Ihren Dienstkontoschlüssel enthält. Diese Variable gilt nur für Ihre aktuelle Shell-Sitzung. Wenn Sie eine neue Sitzung öffnen, müssen Sie die Variable neu festlegen.
Linux oder macOS
export GOOGLE_APPLICATION_CREDENTIALS="[PATH]"
Beispiel:
export GOOGLE_APPLICATION_CREDENTIALS="/home/user/Downloads/my-key.json"
Windows
Mit PowerShell:
$env:GOOGLE_APPLICATION_CREDENTIALS="[PATH]"
Beispiel:
$env:GOOGLE_APPLICATION_CREDENTIALS="C:\Users\username\Downloads\my-key.json"
Mit Eingabeaufforderung:
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.