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)
- Cloud Translation (GA)
This client supports the following Google Cloud Platform services at a Beta quality level:
- BigQuery (Beta)
- Cloud Firestore (Beta)
- Cloud Pub/Sub (Beta)
- Cloud Spanner (Beta)
- Cloud Natural Language (Beta)
- Cloud Video Intelligence (Beta)
- Cloud Vision (Beta)
This client supports the following Google Cloud Platform services at an Alpha quality level:
- Cloud Compute (Alpha)
- Cloud Data Loss Prevention (Alpha)
- Cloud DNS (Alpha)
- Stackdriver Error Reporting (Alpha)
- Stackdriver Monitoring (Alpha)
- Cloud Resource Manager (Alpha)
- Cloud Speech (Alpha)
- Cloud Trace (Alpha)
Note: google-cloud-java is a work-in-progress, and may occasionally make backwards-incompatible changes.
If you are using Maven, add this to your pom.xml file
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud</artifactId>
<version>0.25.0-alpha</version>
</dependency>If you are using Gradle, add this to your dependencies
compile 'com.google.cloud:google-cloud:0.25.0-alpha'If you are using SBT, add this to your dependencies
libraryDependencies += "com.google.cloud" % "google-cloud" % "0.25.0-alpha"For running on Google App Engine, see more instructions here.
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
In cases where the library may expect a project ID explicitly, we provide a helper that can provide the inferred project ID:
import com.google.cloud.ServiceOptions;
...
String projectId = ServiceOptions.getDefaultProjectId();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 Application Default
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
To get help, follow the instructions in the shared Troubleshooting document.
Clients in this repository use either HTTP or gRPC for the transport layer. The README of each client documents the transport layer the client uses.
For HTTP clients, a proxy can be configured by using http.proxyHost and
related system properties as documented by
Java Networking and Proxies.
For gRPC clients, a proxy can be configured by using the
GRPC_PROXY_EXP environment variable as documented by
the gRPC release notes.
Please note that gRPC proxy support is currently experimental.