如何使用 Python API 在不同的 Google Cloud Platform 项目中为主题创建 PubSub 订阅?

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

API 似乎允许我为不同项目中的主题创建订阅,但是当我检查新创建的订阅时,它与主题所在的项目相关联。

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))

这不能通过网络控制台完成。还有另一个API吗?还是我遗漏了什么?

我正在尝试遵循此 API 参考: https://googlecloudplatform.github.io/google-cloud-python/stable/pubsub-subscription.html

我运行这是一个本地执行的Python脚本。 默认项目(gcloud config get-value project)是receiver-project。

通常,云 Pub/Sub 服务支持 cross-project 订阅,但是,旧版本的 python 客户端库对此支持有限。此问题已在较新版本中修复。这会像这样工作:

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)

您也可以使用 gcloud CLI 执行此操作。查看 gcloud pubsub subscriptions --help

Cloud Console 网络 UI 目前不支持此功能。