Google Cloud Client Library for Java
Java idiomatic client for Google Cloud Platform services.
This library supports the following Google Cloud Platform services with clients at a GA quality level:
- BigQuery (GA)
- Cloud Datastore (GA)
- Cloud Natural Language (GA)
- Cloud Pub/Sub (GA)
- Cloud Storage (GA)
- Cloud Translation (GA)
- Cloud Vision (GA)
- Stackdriver Logging (GA)
- Stackdriver Monitoring (GA)
This library supports the following Google Cloud Platform services with clients at a Beta quality level:
- BigQuery Data Transfer (Beta)
- Cloud AutoML (Beta)
- Cloud Container Analysis (Beta)
- Cloud Data Loss Prevention (Beta)
- Cloud Firestore (Beta)
- Cloud IoT Core (Beta)
- Cloud KMS (Beta)
- Cloud Spanner (Beta)
- Cloud Speech (Beta)
- Cloud Text-to-Speech (Beta)
- Cloud Video Intelligence (Beta)
- Kubernetes Engine (Beta)
- Stackdriver Error Reporting (Beta)
- Stackdriver Trace (Beta)
This library supports the following Google Cloud Platform services with clients at an Alpha quality level:
- Cloud Bigtable (Alpha)
- Cloud Bigtable Admin (Alpha)
- Cloud Compute (Alpha)
- Cloud Dataproc (Alpha)
- Cloud DNS (Alpha)
- Cloud OS Login (Alpha)
- Cloud Memorystore for Redis (Alpha)
- Cloud Resource Manager (Alpha)
- Cloud Security Scanner (Alpha)
- Dialogflow (Alpha)
To call any of the supported Google Cloud Services simply add a corresponding client library artifact as a dependency to your project. The following instructions use
google-cloud-storage as an example (specific instructions can be found in the README of each client).
If you are using Maven, add this to your pom.xml file
<dependency> <groupId>com.google.cloud</groupId> <artifactId>google-cloud-storage</artifactId> <version>1.40.0</version> </dependency>
If you are using Gradle, add this to your dependencies
If you are using SBT, add this to your dependencies
libraryDependencies += "com.google.cloud" % "google-cloud-storage" % "1.40.0"
If you're using IntelliJ or Eclipse, you can add client libraries to your project using these IDE plugins:
Besides adding client libraries, the plugins provide additional functionality, such as service account key management. Refer to the documentation for each plugin for more details.
These client libraries can be used on App Engine standard for Java 8 runtime and App Engine flexible (including the Compat runtime). Most of the libraries do not work on the App Engine standard for Java 7 runtime. However, Datastore, Storage, and Bigquery should work.
If you are running into problems with version conflicts, see Version Management.
Specifying a Project ID
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
- The App Engine / Compute Engine project ID
- The project ID specified in the JSON credentials file pointed by the
- The Google Cloud SDK 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();
to authenticate requests.
google-auth-library-java supports a wide range of authentication types;
see the project's README
and javadoc for more
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:
- 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 then
google-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(GoogleCredentials.create(new AccessToken(accessToken, expirationTime))) .build() .getService();
If no credentials are provided,
google-cloud will attempt to detect them from the environment
GoogleCredentials.getApplicationDefault() which will search for Application Default
Credentials in the following locations (in order):
- The credentials file pointed to by the
- 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 Troubleshooting document.
Using a proxy
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
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.
Java 7 or above is required for using the clients in this repository.
Clients in this repository use either HTTP or gRPC for the transport layer. All HTTP-based clients should work in all environments.
For clients that use gRPC, the supported platforms are constrained by the platforms that Forked Tomcat Native supports, which for architectures means only x86_64, and for operating systems means Mac OS X, Windows, and Linux. Additionally, gRPC constrains the use of platforms with threading restrictions.
Thus, the following are not supported:
- Alpine Linux (due to netty-tcnative requiring glibc, which is not present on Alpine)
- Raspberry Pi (since it runs on the ARM architecture)
- Google App Engine Standard Java 7
The following environments should work (among others):
- standalone Windows on x86_64
- standalone Mac OS X on x86_64
- standalone Linux on x86_64
- Google Compute Engine (GCE)
- Google Container Engine (GKE)
- Google App Engine Standard Java 8 (GAE Std J8)
- Google App Engine Flex (GAE Flex)
This library provides tools to help write tests for code that uses google-cloud services.
See TESTING to read more about using our testing helpers.
This library follows Semantic Versioning, but with some additional qualifications:
Components marked with
@BetaApiare considered to be "0.x" features inside a "1.x" library. This means they can change between minor and patch releases in incompatible ways. These features should not be used by any library "B" that itself has consumers, unless the components of library B that use
@BetaApifeatures are also marked with
@BetaApi. Features marked as
@BetaApiare on a path to eventually become "1.x" features with the marker removed.
Special exception for google-cloud-java: google-cloud-java is allowed to depend on
@BetaApifeatures in gax-java without declaring the consuming code
@BetaApi, because gax-java and google-cloud-java move in step with each other. For this reason, gax-java should not be used independently of google-cloud-java.
Components marked with
@InternalApiare technically public, but are only public for technical reasons, because of the limitations of Java's access modifiers. For the purposes of semver, they should be considered private.
Please note it is currently under active development. Any release versioned 0.x.y is subject to backwards incompatible changes at any time.
GA: Libraries defined at a GA quality level are expected to be stable and all updates in the libraries are guaranteed to be backwards-compatible. Any backwards-incompatible changes will lead to the major version increment (1.x.y -> 2.0.0).
Beta: Libraries defined at a Beta quality level are expected to be mostly stable and we're working towards their release candidate. We will address issues and requests with a higher priority.
Alpha: Libraries defined at an Alpha quality level are still a work-in-progress and are more likely to get backwards-incompatible updates. Additionally, it's possible for Alpha libraries to get deprecated and deleted before ever being promoted to Beta or GA.
The easiest way to solve version conflicts is to use google-cloud's BOM. In Maven, add the following to your POM:
<dependencyManagement> <dependencies> <dependency> <groupId>com.google.cloud</groupId> <artifactId>google-cloud-bom</artifactId> <version>0.58.0-alpha</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
This BOM is only available starting at version 0.32.0-alpha. If you are having problems with prior versions of google-cloud, use the following table as a reference to make sure that your versions are compatible. Definitions:
- alpha: The version of any alpha package in google-cloud
- beta: The version of any beta package in google-cloud
- GA: The version of any GA package in google-cloud
- gax: The version of com.google.api:gax
- gax-grpc: The version of com.google.api:gax-grpc
Something to be aware of is that a package can be promoted from alpha -> beta or beta -> GA between versions, which means that after a certain point for any given package, the alpha or beta version won't be valid any more.
Contributions to this library are always welcome and highly encouraged.
Please note that this project is released with a Contributor Code of Conduct. By participating in this project you agree to abide by its terms. See Code of Conduct for more information.
Apache 2.0 - See LICENSE for more information.