There seems to have been a divergent path of cloud and api client libraries in for Google Cloud Platform services. In the api client libraries we could use default credentials but I can't find the documentation to use default credentials in the cloud libraries.
Can we still use the default credentials in the cloud libraries? If not is the suggested path to generate a service user with api keys for a project?
For both Cloud Storage and Stackdriver monitoring client libraries, you should be able to use application default credentials by default just like any other Google client libraries.
From the documentation on github:
If no credentials are provided, google-cloud will attempt to detect them from the environment using
GoogleCredentials.getApplicationDefault()
which will search for Default Application Credentials in the following locations (in order):
- The credentials file pointed to by the
GOOGLE_APPLICATION_CREDENTIALS
environment variable.- Credentials provided by the Google Cloud SDK
gcloud auth application-default login
command.- Google App Engine built-in credentials.
- Google Cloud Shell built-in credentials Google
- Compute Engine built-in credentials
Depending on your setup and environment, you can choose the method which works best. Usually the environment variable GOOGLE_APPLICATION_CREDENTIALS
pointing to the credentials json file is easiest to set up.
Once you've done the above, you can go ahead and call the respective libraries.
For Cloud Storage (copying the example 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());
}
}
For Stackdriver monitoring (copying the example 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();
}
}
BTW, Cloud Monitoring libraries APIs v2 are deprecated in favor of Stackdriver Monitoring libraries and APIs v3.