3

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

这无法通过 Web 控制台完成。还有其他 API 吗?还是我错过了什么?

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

我将其作为本地执行的 Python 脚本运行。默认项目(gcloud config get-value 项目)是接收者项目。

4

1 回答 1

2

通常,Cloud Pub/Sub 服务支持跨项目订阅,但是在旧版本的 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 网页界面目前不支持此功能。

于 2018-03-21T15:56:27.630 回答