使用 com.google.cloud.google-cloud 库 ( http://googlecloudplatform.github.io/google-cloud-java/0.21.1/index.html ),我有以下用于 Google Cloud Platform 发布/订阅的代码:
TopicName topicName = TopicName.create("projectId...", "topic...");
Publisher pub = Publisher.defaultBuilder(topicName).build();
我如何提供发布者使用的凭据?我已经将它们保存在内存中,因为它们是通过其他方式提供的,而不是将它们硬编码到文件中。
所有使用自定义凭证的例子如下:
Storage storage = StorageOptions.newBuilder()
.setProjectId(PROJECT_ID)
.setCredentials(GoogleCredentials.fromStream(
new FileInputStream(PATH_TO_JSON_KEY))).build();
Bucket bucket = storage.create(BucketInfo.of("myBucketName"));
但是没有 PubSubOptions 或 PublisherOptions 类来编写类似的代码(我将 FileInputStream 替换为 ByteArrayInputStream)。
有人遇到同样的问题,但代码示例不起作用:https://github.com/GoogleCloudPlatform/google-cloud-java/issues/1751
最佳答案
您可以在构建器上设置凭据提供程序:
GoogleCredentials credentials = GoogleCredentials.fromStream(
new FileInputStream(PATH_TO_JSON_KEY)));
Publisher pub = Publisher
.defaultBuilder(topicName)
.setCredentialsProvider(FixedCredentialsProvider.create(credentials))
.build();
现在将设置使用的 CallSettings
...但我认为它不会设置在设置 channel 本身时使用的凭据。 (看起来Java gRPC代码和我比较熟悉的C#代码之间可能存在差异。)
关于java - Google Cloud Platform pub/sub Publisher,如何提供默认应用程序凭据以外的凭据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45562427/