简体   繁体   中英

Access a foreign project's PubSub with com.google.cloud.pubsub API

I have an AppEngine app that publishes messages to PubSub. Now I want to read those messages from a different GCP project. This is possible using com.google.apis/google-api-services-pubsub v1-rev8-1.21.0 and a service-account key.

I took the examples from https://github.com/GoogleCloudPlatform/cloud-pubsub-samples-java as a guide. Parts of the code are a bit ugly (eg requiring a RetryHttpInitializerWrapper) so I was wondering if it's possible to achieve the same using com.google.cloud/google-cloud-pubsub v. 0.4.0

However I couldn't figure out how to specify a different project. All examples simply use

PubSub pubsub = PubSubOptions.defaultInstance().service();

which gives me my own project's instances. How can I tell PubSub to use a different projectId?

In v0.4.0 of the google-cloud-java library, you can use the PubSubOptions.Builder to create your PubSubOptions. You would use the projectId method to set the project. Note that in v0.5.0, this method was deprecated in favor of setProjectId .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM