【问题标题】:Google Cloud Platform - unified library谷歌云平台——统一库
【发布时间】:2016-06-19 17:28:42
【问题描述】:

我已经用 Java 启动了新项目,并希望将其部署在 Google Cloud Platform(而不是 App Engine!)上。我也想使用 GCP 提供的一些服务/产品:Datastore、PubSub 等。

我从 Datastore 开始,发现了一个不错的库: https://github.com/GoogleCloudPlatform/gcloud-java

然后我决定开始使用 PubSub,发现 gcloud-java 没有任何与 PubSub 相关的内容。 Java PubSub 支持的一些库在这里: https://cloud.google.com/pubsub/libraries

所以,我只是想确认一下(因为这对我来说太奇怪了),Google 没有任何统一的 Java 库来提供他们的服务?除了使用两个库(使用不同的凭据管理策略等)之外,我别无他法?

任何帮助/建议将不胜感激! 提前谢谢你。

【问题讨论】:

    标签: java google-app-engine


    【解决方案1】:

    我认为您根本不需要第一个库。您只需要以下内容:

    compile "com.google.api-client:google-api-client:1.21.0"
    compile "com.google.apis:google-api-services-pubsub:v1-rev8-1.21.0"
    

    你的代码会是这样的:

    HttpTransport httpTransport = new ApacheHttpTransport();
    JsonFactory jsonFactory = JacksonFactory.getDefaultInstance()
    GoogleCredential credentials = GoogleCredential
          .fromStream(json)
          .createScoped(PubsubScopes.all());
    
    // or on Google Cloud Platform
    GoogleCredential credentials = GoogleCredential
          .getApplicationDefault(httpTransport, jsonFactory);
    
    Pubsub pubsub = new Pubsub.Builder(httpTransport, jsonFactory, credentials)
          .build();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-26
      • 2020-06-02
      • 1970-01-01
      • 1970-01-01
      • 2020-12-11
      • 1970-01-01
      • 2020-08-06
      • 1970-01-01
      相关资源
      最近更新 更多