Skip to content

Latest commit

 

History

History

cloud-client

Google Cloud Pub/Sub Python Samples

This directory contains samples for Google Cloud Pub/Sub. Google Cloud Pub/Sub is a fully-managed real-time messaging service that allows you to send and receive messages between independent applications.

Setup

Authentication

Authentication is typically done through Application Default Credentials, which means you do not have to change the code to authenticate as long as your environment has credentials. You have a few options for setting up authentication:

  1. When running locally, use the Google Cloud SDK

    gcloud beta auth application-default login
  2. When running on App Engine or Compute Engine, credentials are already set-up. However, you may need to configure your Compute Engine instance with additional scopes.

  3. You can create a Service Account key file. This file can be used to authenticate to Google Cloud Platform services from any environment. To use the file, set the GOOGLE_APPLICATION_CREDENTIALS environment variable to the path to the key file, for example:

    export GOOGLE_APPLICATION_CREDENTIALS=/path/to/service_account.json

Install Dependencies

  1. Install pip and virtualenv if you do not already have them.

  2. Create a virtualenv. Samples are compatible with Python 2.7 and 3.4+.

    $ virtualenv env
    $ source env/bin/activate
  3. Install the dependencies needed to run the samples.

    $ pip install -r requirements.txt

Samples

Quickstart

To run this sample:

$ python quickstart.py

Publisher

To run this sample:

$ python publisher.py

usage: publisher.py [-h] {list,create,delete,publish} ...

This application demonstrates how to perform basic operations on topics
with the Cloud Pub/Sub API.

For more information, see the README.md under /pubsub and the documentation
at https://cloud.google.com/pubsub/docs.

positional arguments:
  {list,create,delete,publish}
    list                Lists all Pub/Sub topics in the current project.
    create              Create a new Pub/Sub topic.
    delete              Deletes an existing Pub/Sub topic.
    publish             Publishes a message to a Pub/Sub topic with the given
                        data.

optional arguments:
  -h, --help            show this help message and exit

Subscribers

To run this sample:

$ python subscriber.py

usage: subscriber.py [-h] {list,create,delete,receive} ...

This application demonstrates how to perform basic operations on
subscriptions with the Cloud Pub/Sub API.

For more information, see the README.md under /pubsub and the documentation
at https://cloud.google.com/pubsub/docs.

positional arguments:
  {list,create,delete,receive}
    list                Lists all subscriptions for a given topic.
    create              Create a new pull subscription on the given topic.
    delete              Deletes an existing Pub/Sub topic.
    receive             Receives a message from a pull subscription.

optional arguments:
  -h, --help            show this help message and exit

Identity and Access Management

To run this sample:

$ python iam.py

usage: iam.py [-h]
              {get-topic-policy,get-subscription-policy,set-topic-policy,set-subscription-policy,check-topic-permissions,check-subscription-permissions}
              ...

This application demonstrates how to perform basic operations on IAM
policies with the Cloud Pub/Sub API.

For more information, see the README.md under /pubsub and the documentation
at https://cloud.google.com/pubsub/docs.

positional arguments:
  {get-topic-policy,get-subscription-policy,set-topic-policy,set-subscription-policy,check-topic-permissions,check-subscription-permissions}
    get-topic-policy    Prints the IAM policy for the given topic.
    get-subscription-policy
                        Prints the IAM policy for the given subscription.
    set-topic-policy    Sets the IAM policy for a topic.
    set-subscription-policy
                        Sets the IAM policy for a topic.
    check-topic-permissions
                        Checks to which permissions are available on the given
                        topic.
    check-subscription-permissions
                        Checks to which permissions are available on the given
                        subscription.

optional arguments:
  -h, --help            show this help message and exit

The client library

This sample uses the `Google Cloud Client Library for Python`_. You can read the documentation for more details on API usage and use GitHub to `browse the source`_ and `report issues`_.