Java idiomatic client for Google Cloud Platform services.
This client supports the following Google Cloud Platform services at a GA quality level:
- Stackdriver Logging (GA)
- Cloud Datastore (GA)
- Cloud Storage (GA)
This client supports the following Google Cloud Platform services at a Beta quality level:
- BigQuery (Beta)
- Cloud Spanner (Beta)
- Cloud Translation (Beta)
- Cloud Natural Language (Beta)
- Cloud Vision (Beta)
This client supports the following Google Cloud Platform services at an Alpha quality level:
- Cloud Compute (Alpha)
- Cloud DNS (Alpha)
- Stackdriver Error Reporting (Alpha)
- Stackdriver Monitoring (Alpha)
- Cloud Pub/Sub (Alpha)
- Cloud Resource Manager (Alpha)
- Cloud Speech (Alpha)
- Cloud Trace (Alpha)
- Cloud Video Intelligence (Alpha)
Note: This client is a work-in-progress, and may occasionally make backwards-incompatible changes.
gcloud-java lives on under a new name, google-cloud.
Your code will behave the same, simply change your dependency (see Quickstart).
If you are using Maven, add this to your pom.xml file
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud</artifactId>
<version>0.20.0-alpha</version>
</dependency>If you are using Gradle, add this to your dependencies
compile 'com.google.cloud:google-cloud:0.20.0-alpha'If you are using SBT, add this to your dependencies
libraryDependencies += "com.google.cloud" % "google-cloud" % "0.20.0-alpha"For running on Google App Engine, see more instructions here.
BigQueryExample- A simple command line interface providing some of Cloud BigQuery's functionality- Read more about using this application on the
BigQueryExampledocs page.
- Read more about using this application on the
ComputeExample- A simple command line interface providing some of Cloud Compute's functionality- Read more about using this application on the
google-cloud-examplesdocs page.
- Read more about using this application on the
Bookshelf- An App Engine app that manages a virtual bookshelf.- This app uses
google-cloudto interface with Cloud Datastore and Cloud Storage. It also uses Cloud SQL, another Google Cloud Platform service.
- This app uses
DatastoreExample- A simple command line interface for Cloud Datastore- Read more about using this application on the
DatastoreExampledocs page.
- Read more about using this application on the
DnsExample- A simple command line interface for Cloud DNS- Read more about using this application on the
DnsExampledocs page.
- Read more about using this application on the
Flexible Environment/Datastore example- A simple app that uses Cloud Datastore to list the last 10 IP addresses that visited your site.- Read about how to run the application here.
Flexible Environment/Storage example- An app that uploads files to a public Cloud Storage bucket on the App Engine Flexible Environment runtime.GuestBook- An App Engine Standard guestbook that uses Cloud Datastore.LoggingExample- A simple command line interface providing some of Stackdriver Logging's functionality- Read more about using this application on the
LoggingExampledocs page.
- Read more about using this application on the
ResourceManagerExample- A simple command line interface providing some of Cloud Resource Manager's functionality- Read more about using this application on the
ResourceManagerExampledocs page.
- Read more about using this application on the
SparkDemo- An example of usinggoogle-cloud-datastorefrom within the SparkJava and App Engine Flexible Environment frameworks.- Read about how it works on the example's README page.
StorageExample- A simple command line interface providing some of Cloud Storage's functionality- Read more about using this application on the
StorageExampledocs page.
- Read more about using this application on the
TaskList- A command line application that uses Cloud Datastore to manage a to-do list.- Read about how to run the application on its README page.
TranslateExample- A simple command line interface providing some of Google Translation's functionality- Read more about using this application on the
TranslateExampledocs page.
- Read more about using this application on the
Most google-cloud libraries require a project ID. There are multiple ways to specify this project ID.
- When using
google-cloudlibraries from within Compute/App Engine, there's no need to specify a project ID. It is automatically inferred from the production environment. - When using
google-cloudelsewhere, you can do one of the following:
- Supply the project ID when building the service options. For example, to use Datastore from a project with ID "PROJECT_ID", you can write:
Datastore datastore = DatastoreOptions.newBuilder().setProjectId("PROJECT_ID").build().getService();- Specify the environment variable
GOOGLE_CLOUD_PROJECTto be your desired project ID. - Set the project ID using the Google Cloud SDK. To use the SDK, download the SDK if you haven't already, and set the project ID from the command line. For example:
gcloud config set project PROJECT_ID
google-cloud determines the project ID from the following sources in the listed order, stopping once it finds a value:
- The project ID supplied when building the service options
- Project ID specified by the environment variable
GOOGLE_CLOUD_PROJECT - The App Engine project ID
- The project ID specified in the JSON credentials file pointed by the
GOOGLE_APPLICATION_CREDENTIALSenvironment variable - The Google Cloud SDK project ID
- The Compute Engine project ID
google-cloud-java uses
https://github.com/google/google-auth-library-java
to authenticate requests. google-auth-library-java supports a wide range of authentication types;
see the project's README
and javadoc for more
details.
To access Google Cloud services, you first need to ensure that the necessary Google Cloud APIs are enabled for your project. To do this, follow the instructions on the authentication document shared by all the Google Cloud language libraries.
Next, choose a method for authenticating API requests from within your project:
- When using
google-cloudlibraries from within Compute/App Engine, no additional authentication steps are necessary. For example:
Storage storage = StorageOptions.getDefaultInstance().getService();- When using
google-cloudlibraries elsewhere, there are several options:
- Generate a JSON service account key.
After downloading that key, you must do one of the following:
- Define the environment variable GOOGLE_APPLICATION_CREDENTIALS to be the location of the key. For example:
export GOOGLE_APPLICATION_CREDENTIALS=/path/to/my/key.json- Supply the JSON credentials file when building the service options. For example, this Storage object has the necessary permissions to interact with your Google Cloud Storage data:
Storage storage = StorageOptions.newBuilder() .setCredentials(ServiceAccountCredentials.fromStream(new FileInputStream("/path/to/my/key.json"))) .build() .getService();
- If running locally for development/testing, you can use the
Google Cloud SDK. Create Application Default Credentials with
gcloud auth application-default login, and thengoogle-cloudwill automatically detect such credentials. - If you already have an OAuth2 access token, you can use it to authenticate (notice that in this case, the access token will not be automatically refreshed):
Storage storage = StorageOptions.newBuilder()
.setCredentials(new GoogleCredentials(new AccessToken(accessToken, expirationTime)))
.build()
.getService();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_CREDENTIALSenvironment variable - Credentials provided by the Google Cloud SDK
gcloud auth application-default logincommand - Google App Engine built-in credentials
- Google Cloud Shell built-in credentials
- Google Compute Engine built-in credentials
Follow the activation instructions to use the Stackdriver Logging API with your project.
Here are two code snippets showing simple usage examples from within Compute Engine/App Engine Flexible. Note that you must supply credentials and a project ID if running this snippet elsewhere.
The first snippet shows how to write and list log entries. Complete source code can be found on WriteAndListLogEntries.java.
import com.google.cloud.MonitoredResource;
import com.google.cloud.Page;
import com.google.cloud.logging.LogEntry;
import com.google.cloud.logging.Logging;
import com.google.cloud.logging.Logging.EntryListOption;
import com.google.cloud.logging.LoggingOptions;
import com.google.cloud.logging.Payload.StringPayload;
import java.util.Collections;
import java.util.Iterator;
LoggingOptions options = LoggingOptions.getDefaultInstance();
try(Logging logging = options.getService()) {
LogEntry firstEntry = LogEntry.newBuilder(StringPayload.of("message"))
.setLogName("test-log")
.setResource(MonitoredResource.newBuilder("global")
.addLabel("project_id", options.getProjectId())
.build())
.build();
logging.write(Collections.singleton(firstEntry));
Page<LogEntry> entries = logging.listLogEntries(
EntryListOption.filter("logName=projects/" + options.getProjectId() + "/logs/test-log"));
Iterator<LogEntry> entryIterator = entries.iterateAll();
while (entryIterator.hasNext()) {
System.out.println(entryIterator.next());
}
}The second snippet shows how to use a java.util.logging.Logger to write log entries to Stackdriver
Logging. The snippet installs a Stackdriver Logging handler using
LoggingHandler.addHandler(Logger, LoggingHandler). Notice that this could also be done through the
logging.properties file, adding the following line:
com.google.cloud.examples.logging.snippets.AddLoggingHandler.handlers=com.google.cloud.logging.LoggingHandler