0

Google Cloud Platformサービスでは、クラウドとapiのクライアントライブラリの相違するパスが存在するようです。 apiクライアントライブラリではデフォルトの資格情報を使用できますが、クラウドライブラリでデフォルトの資格情報を使用するためのドキュメントが見つかりません。GCPクライアントとクラウドAPIの認証

クラウドライブラリのデフォルトの認証情報を引き続き使用できますか?そうでない場合は、プロジェクトのapiキーを持つサービスユーザを生成するための推奨パスはありますか?

+0

はあなたがここに言及されているものをクラウドライブラリについて、より具体的だろうか? – Tuxdude

+0

@Tuxdudeが明確化を求めてくれてありがとう、私はそれぞれ、特にcloudstorageとのStackdriverライブラリ用に関するのでグーグル・クラウドの監視とGoogle-cloudstorageライブラリを求めています。 – user3325789

答えて

1

Cloud StorageとStackdriverの両方のクライアントライブラリを監視する場合、既定で他のGoogleクライアントライブラリと同様にアプリケーションのデフォルトの認証情報を使用できるはずです。 documentation on githubから

:何の資格情報が提供されていない場合

、グーグル・クラウドは、順番に次の場所(中 デフォルトのアプリケーションの資格情報を検索します GoogleCredentials.getApplicationDefault()を使用して、環境から にそれらを検出しようとします):

  1. 環境変数GOOGLE_APPLICATION_CREDENTIALSが示す資格ファイルです。
  2. Google Cloud SDK gcloud auth application-default loginコマンドによって提供された資格情報。
  3. Google App Engineの組み込み資格情報。
  4. Googleクラウドシェルは、組み込みの資格情報をGoogleの
  5. Compute Engineの組み込みの資格情報

セットアップや環境に応じて、最適な方法を選択することができます。通常、資格情報のjsonファイルを指す環境変数GOOGLE_APPLICATION_CREDENTIALSは設定が簡単です。あなたが上記を完了したら

、あなたは先に行くと、それぞれのライブラリを呼び出すことができます。クラウドストレージのために

(例hereをコピー):のStackdriverの監視のために

// 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()); 
    } 
} 

(例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(); 
    } 
} 

ところで、クラウド・モニタリング・ライブラリAPIs v2はのStackdriverの賛成で廃止されました監視ライブラリとAPIs v3

関連する問題