Google 云平台 pub/sub 发布者,如何提供除默认应用程序凭据以外的凭据
Google Cloud Platform pub/sub Publisher, how to supply credentials other than default application credentials
使用 com.google.cloud.google-云库 (http://googlecloudplatform.github.io/google-cloud-java/0.21.1/index.html),我有以下 Google 云平台 pub/sub 的代码:
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 class 来编写类似的代码(我将 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
使用...但我不 认为 它会设置设置频道本身时使用的凭据。 (看起来 Java gRPC 代码和我更熟悉的 C# 代码之间可能存在差异。)
界面略有变化。您现在应该使用:
GoogleCredentials credentials = GoogleCredentials.fromStream(new FileInputStream("<path-to-service-account JSON file>"));
Publisher publisher = Publisher
.newBuilder(topicName)
.setCredentialsProvider(FixedCredentialsProvider.create(credentials))
.build();
使用 com.google.cloud.google-云库 (http://googlecloudplatform.github.io/google-cloud-java/0.21.1/index.html),我有以下 Google 云平台 pub/sub 的代码:
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 class 来编写类似的代码(我将 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
使用...但我不 认为 它会设置设置频道本身时使用的凭据。 (看起来 Java gRPC 代码和我更熟悉的 C# 代码之间可能存在差异。)
界面略有变化。您现在应该使用:
GoogleCredentials credentials = GoogleCredentials.fromStream(new FileInputStream("<path-to-service-account JSON file>"));
Publisher publisher = Publisher
.newBuilder(topicName)
.setCredentialsProvider(FixedCredentialsProvider.create(credentials))
.build();