Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I create a PubSub subscription for a topic in a different Google Cloud Platform project using the Python API?

The API seems to allow me to create a subscription for a topic in a different project, but when I inspect the newly created subscription, the it is associated with the project where the topic is located.

from google.cloud import pubsub

pubsub_client_publisher = pubsub.Client("publisher-project")
topic = pubsub_client_publisher.topic("topic1")

pubsub_client_receiver = pubsub.Client("receiver-project")
subscription = pubsub.subscription.Subscription("subscription1", topic)
subscription.create(pubsub_client_receiver);    # the argument is ignored

print('Subscription {} created on topic {}.'.format(
    subscription.full_name, topic.full_name))

This cannot be done via the web console. Is there another API? Or am I missing something?

I am trying to follow this API reference: https://googlecloudplatform.github.io/google-cloud-python/stable/pubsub-subscription.html

I am running this as a locally executed Python script. The default project (gcloud config get-value project) is the receiver-project.

like image 425
Jan Dolejsi Avatar asked Sep 20 '25 07:09

Jan Dolejsi


1 Answers

Generally, the Cloud Pub/Sub service supports cross-project subscriptions, however, there was limited support for this in the older version of the python client library. This issue is fixed in the newer versions. This would work something like:

from google.cloud import pubsub_v1 as pubsub
c = pubsub.SubscriberClient()
t = c.topic_path('topic_project', 'topic_name')
s = c.subscription_path('subscription_project', 'subscription_path')
c.create_subscription(s,t)

You can also do this with the gcloud CLI. Check out gcloud pubsub subscriptions --help

The Cloud Console web UI does not currently support this.

like image 125
Kir Titievsky Avatar answered Sep 22 '25 16:09

Kir Titievsky