繁体   English   中英

Google Cloud Platform pub / sub Publisher,如何提供默认应用程序凭据以外的凭据

[英]Google Cloud Platform pub/sub Publisher, how to supply credentials other than default application credentials

使用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 ...但我认为它不会设置设置频道本身时使用的凭据。 (看起来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();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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