【问题标题】:GCP Client vs Cloud API AuthenticationGCP 客户端与云 API 身份验证
【发布时间】:2017-06-07 16:04:35
【问题描述】:

对于 Google Cloud Platform 服务,云和 api 客户端库的路径似乎有所不同。在 api 客户端库中,我们可以使用默认凭据,但我找不到在云库中使用默认凭据的文档。

我们还可以使用云库中的默认凭据吗?如果不是,建议使用项目的 api 密钥生成服务用户的路径吗?

【问题讨论】:

  • 您能否更具体地说明您在这里指的是哪些云库?
  • @Tuxdude 感谢您的澄清,我特别询问了 cloudstorage 和 stackdriver 库,因此分别是 google-cloud-monitoring 和 google-cloud-storage 库。

标签: python google-cloud-platform google-cloud-storage google-client


【解决方案1】:

对于 Cloud Storage 和 Stackdriver 监控客户端库,您应该能够默认使用应用默认凭据,就像任何其他 Google 客户端库一样。

来自documentation on github

如果没有提供凭据,google-cloud 将尝试检测 他们从环境中使用 GoogleCredentials.getApplicationDefault() 将搜索 以下位置的默认应用程序凭据(按顺序):

  1. GOOGLE_APPLICATION_CREDENTIALS 环境变量指向的凭据文件。
  2. Google Cloud SDK gcloud auth application-default login 命令提供的凭据。
  3. Google App Engine 内置凭据。
  4. Google Cloud Shell 内置凭据 Google
  5. Compute Engine 内置凭据

根据您的设置和环境,您可以选择最有效的方法。通常环境变量GOOGLE_APPLICATION_CREDENTIALS指向凭证json文件是最容易设置的。

完成上述操作后,您可以继续调用相应的库。

对于云存储(复制示例here):

// Imports the Google Cloud client library
import com.google.cloud.storage.Bucket;
import com.google.cloud.storage.BucketInfo;
import com.google.cloud.storage.Storage;
import com.google.cloud.storage.StorageOptions;

public class QuickstartSample {
  public static void main(String... args) throws Exception {
    // Instantiates a client
    Storage storage = StorageOptions.getDefaultInstance().getService();

    // The name for the new bucket
    String bucketName = args[0];  // "my-new-bucket";

    // Creates the new bucket
    Bucket bucket = storage.create(BucketInfo.of(bucketName));

    System.out.printf("Bucket %s created.%n", bucket.getName());
  }
}

对于 Stackdriver 监控(复制示例 here):

import com.google.api.Metric;
import com.google.api.MonitoredResource;

// Imports the Google Cloud client library
import com.google.cloud.monitoring.spi.v3.MetricServiceClient;

import com.google.monitoring.v3.CreateTimeSeriesRequest;
import com.google.monitoring.v3.Point;
import com.google.monitoring.v3.ProjectName;
import com.google.monitoring.v3.TimeInterval;
import com.google.monitoring.v3.TimeSeries;
import com.google.monitoring.v3.TypedValue;
import com.google.protobuf.util.Timestamps;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class QuickstartSample {
  public static void main(String... args) throws Exception {
    // Your Google Cloud Platform project ID
    String projectId = System.getProperty("projectId");

    if (projectId == null) {
      System.err.println("Usage: QuickstartSample -DprojectId=YOUR_PROJECT_ID");
      return;
    }

    // Instantiates a client
    MetricServiceClient metricServiceClient = MetricServiceClient.create();

    // Prepares an individual data point
    TimeInterval interval = TimeInterval.newBuilder()
        .setEndTime(Timestamps.fromMillis(System.currentTimeMillis()))
        .build();
    TypedValue value = TypedValue.newBuilder()
        .setDoubleValue(123.45)
        .build();
    Point point = Point.newBuilder()
        .setInterval(interval)
        .setValue(value)
        .build();

    List<Point> pointList = new ArrayList<>();
    pointList.add(point);

    ProjectName name = ProjectName.create(projectId);

    // Prepares the metric descriptor
    Map<String, String> metricLabels = new HashMap<String, String>();
    metricLabels.put("store_id", "Pittsburg");
    Metric metric = Metric.newBuilder()
        .setType("custom.googleapis.com/stores/daily_sales")
        .putAllLabels(metricLabels)
        .build();

    // Prepares the monitored resource descriptor
    Map<String, String> resourceLabels = new HashMap<String, String>();
    resourceLabels.put("project_id", projectId);
    MonitoredResource resource = MonitoredResource.newBuilder()
        .setType("global")
        .putAllLabels(resourceLabels)
        .build();

    // Prepares the time series request
    TimeSeries timeSeries = TimeSeries.newBuilder()
        .setMetric(metric)
        .setResource(resource)
        .addAllPoints(pointList)
        .build();
    List<TimeSeries> timeSeriesList = new ArrayList<>();
    timeSeriesList.add(timeSeries);

    CreateTimeSeriesRequest request = CreateTimeSeriesRequest.newBuilder()
        .setNameWithProjectName(name)
        .addAllTimeSeries(timeSeriesList)
        .build();

    // Writes time series data
    metricServiceClient.createTimeSeries(request);

    System.out.printf("Done writing time series data.%n");

    metricServiceClient.close();
  }
}

顺便说一句,Cloud Monitoring 库 APIs v2 已弃用,取而代之的是 Stackdriver Monitoring 库和 APIs v3

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-06-21
    • 2018-01-09
    • 2016-01-23
    • 1970-01-01
    • 2013-05-19
    • 2019-10-03
    • 1970-01-01
    相关资源
    最近更新 更多