- Notifications
You must be signed in to change notification settings - Fork1.1k
Google Cloud Client Library for Java
License
googleapis/google-cloud-java
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
Java idiomatic client forGoogle Cloud Platform services.
Libraries are available on GitHub and Maven Central for developing Java applications that interact with individual Google Cloud services:
If the service is not listed,google-api-java-client interfaces with additional Google Cloud APIs using a legacy REST interface.
When building Java applications, preference should be given to the libraries listed in the table.
Mostgoogle-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:
Datastoredatastore =DatastoreOptions.newBuilder().setProjectId("PROJECT_ID").build().getService();
Specify the environment variable
GOOGLE_CLOUD_PROJECTto be your desired project ID.Set the project ID using theGoogle 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 / Compute 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
In cases where the library may expect a project ID explicitly, we provide a helper that can provide the inferred project ID:
importcom.google.cloud.ServiceOptions; ...StringprojectId =ServiceOptions.getDefaultProjectId();
google-cloud-java useshttps://github.com/googleapis/google-auth-library-javato authenticate requests.google-auth-library-java supports a wide range of authentication types;see the project'sREADMEandjavadoc for moredetails.
When using Google Cloud libraries from a Google Cloud Platform environment such as Compute Engine,Kubernetes Engine, or App Engine, no additional authentication steps are necessary.
For example:
Storagestorage =StorageOptions.getDefaultInstance().getService();
or:
CloudTasksClientcloudTasksClient =CloudTasksClient.create();
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 Storageobject has the necessary permissions to interact with your Google Cloud Storage data:
Storagestorage =StorageOptions.newBuilder() .setCredentials(ServiceAccountCredentials.fromStream(newFileInputStream("/path/to/my/key.json"))) .build() .getService();
If running locally for development/testing, you can use theGoogle Cloud SDK.Create Application Default Credentials withgcloud auth application-default login, and thengoogle-cloud will automatically detect such credentials.
If you already have an OAuth2 access token, you can use it to authenticate (notice that in this case, theaccess token will not be automatically refreshed):
Credentialscredentials =GoogleCredentials.create(newAccessToken(accessToken,expirationTime));Storagestorage =StorageOptions.newBuilder() .setCredentials(credentials) .build() .getService();
or:
Credentialscredentials =GoogleCredentials.create(newAccessToken(accessToken,expirationTime));CloudTasksSettingscloudTasksSettings =CloudTasksSettings.newBuilder() .setCredentialProvider(FixedCredentialsProvider.create(credentials)) .build();CloudTasksClientcloudTasksClient =CloudTasksClient.create(cloudTasksSettings);
If no credentials are provided,google-cloud will attempt to detect them from the environmentusingGoogleCredentials.getApplicationDefault() which will search for Application DefaultCredentials 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
Authenticating with API Keys is supported by a handful of Google Cloud APIs.
We are actively exploring ways to improve the API Key experience.Currently, to use an API Key with a Java client library, you need to set the header for the relevant service Client manually.
For example, to set the API Key with theLanguage service:
publicLanguageServiceClientcreateGrpcClientWithApiKey(StringapiKey)throwsException {// Manually set the api key via the headerMap<String,String>header =newHashMap<String,String>() { {put("x-goog-api-key",apiKey);}};FixedHeaderProviderheaderProvider =FixedHeaderProvider.create(header);// Create the clientTransportChannelProvidertransportChannelProvider =InstantiatingGrpcChannelProvider.newBuilder().setHeaderProvider(headerProvider).build();LanguageServiceSettingssettings =LanguageServiceSettings.newBuilder().setTransportChannelProvider(transportChannelProvider).build();LanguageServiceClientclient =LanguageServiceClient.create(settings);returnclient; }
An example instantiation with the Language Client using rest:
publicLanguageServiceClientcreateRestClientWithApiKey(StringapiKey)throwsException {// Manually set the api key headerMap<String,String>header =newHashMap<String,String>() { {put("x-goog-api-key",apiKey);}};FixedHeaderProviderheaderProvider =FixedHeaderProvider.create(header);// Create the clientTransportChannelProvidertransportChannelProvider =InstantiatingHttpJsonChannelProvider.newBuilder().setHeaderProvider(headerProvider).build();LanguageServiceSettingssettings =LanguageServiceSettings.newBuilder().setTransportChannelProvider(transportChannelProvider).build();LanguageServiceClientclient =LanguageServiceClient.create(settings);returnclient; }
To get help, follow the instructions in theTroubleshooting document.
Google Cloud client libraries use HTTPS and gRPC in underlying communicationwith the services.In both protocols, you can configure a proxy usinghttps.proxyHostand (optional)https.proxyPort properties.
For a more custom proxy with gRPC, you will need supply aProxyDetector totheManagedChannelBuilder:
importcom.google.api.core.ApiFunction;importcom.google.api.gax.rpc.TransportChannelProvider;importcom.google.cloud.tasks.v2.CloudTasksClient;importcom.google.cloud.tasks.v2.CloudTasksSettings;importcom.google.cloud.tasks.v2.stub.CloudTasksStubSettings;importio.grpc.HttpConnectProxiedSocketAddress;importio.grpc.ManagedChannelBuilder;importio.grpc.ProxiedSocketAddress;importio.grpc.ProxyDetector;importjavax.annotation.Nullable;importjava.io.IOException;importjava.net.InetSocketAddress;importjava.net.SocketAddress;publicCloudTasksClientgetService()throwsIOException {TransportChannelProvidertransportChannelProvider =CloudTasksStubSettings.defaultGrpcTransportProviderBuilder() .setChannelConfigurator(newApiFunction<ManagedChannelBuilder,ManagedChannelBuilder>() {@OverridepublicManagedChannelBuilderapply(ManagedChannelBuildermanagedChannelBuilder) {returnmanagedChannelBuilder.proxyDetector(newProxyDetector() {@Nullable@OverridepublicProxiedSocketAddressproxyFor(SocketAddresssocketAddress)throwsIOException {returnHttpConnectProxiedSocketAddress.newBuilder() .setUsername(PROXY_USERNAME) .setPassword(PROXY_PASSWORD) .setProxyAddress(newInetSocketAddress(PROXY_HOST,PROXY_PORT)) .setTargetAddress((InetSocketAddress)socketAddress) .build(); } }); } }) .build();CloudTasksSettingscloudTasksSettings =CloudTasksSettings.newBuilder() .setTransportChannelProvider(transportChannelProvider) .build();returnCloudTasksClient.create(cloudTasksSettings);}
Long running operations (LROs) are often used for API calls that are expected totake a long time to complete (i.e. provisioning a GCE instance or a Dataflow pipeline).The initial API call creates an "operation" on the server and returns an Operation IDto track its progress. LRO RPCs have the suffixAsync appended to the call name(i.e.clusterControllerClient.createClusterAsync())
Our generated clients provide a nice interface for starting the operation andthen waiting for the operation to complete. This is accomplished by returning anOperationFuture.When callingget() on theOperationFuture, the client library will poll the operation tocheck the operation's status.
For example, take a samplecreateCluster Operation in google-cloud-dataproc v4.20.0:
try (ClusterControllerClientclusterControllerClient =ClusterControllerClient.create()) {CreateClusterRequestrequest =CreateClusterRequest.newBuilder() .setProjectId("{PROJECT_ID}") .setRegion("{REGION}") .setCluster(Cluster.newBuilder().build()) .setRequestId("{REQUEST_ID}") .setActionOnFailedPrimaryWorkers(FailureAction.forNumber(0)) .build();OperationFuture<Cluster,ClusterOperationMetadata>future =clusterControllerClient.createClusterOperationCallable().futureCall(request);// Do something.Clusterresponse =future.get();}catch (CancellationExceptione) {// Exceeded the default RPC timeout without the Operation completing.// Library is no longer polling for the Operation status. Consider// increasing the timeout.}
The polling operations have a default timeout that varies from service to service.The library will throw ajava.util.concurrent.CancellationException with the message:Task was cancelled. if the timeout exceeds the operation. ACancellationExceptiondoes not mean that the backend GCP Operation was cancelled. This exception is thrown from theclient library when it has exceeded the total timeout without receiving a successful status from the operation.Our client libraries respect the configured values set in the OperationTimedPollAlgorithm for each RPC.
Note: The client library handles the Operation's polling mechanism for you. By default, there is no needto manually poll the status yourself.
Each LRO RPC has a set of pre-configured default values. You can find these values bysearching in each Client'sStubSettings's class. The default LRO settings are initializedinside theinitDefaults() method in the nested Builder class.
For example, in google-cloud-aiplatform v3.24.0, the defaultOperationTimedPollAlgorithmhas these default values:
OperationTimedPollAlgorithm.create(RetrySettings.newBuilder() .setInitialRetryDelay(Duration.ofMillis(5000L)) .setRetryDelayMultiplier(1.5) .setMaxRetryDelay(Duration.ofMillis(45000L)) .setInitialRpcTimeout(Duration.ZERO) .setRpcTimeoutMultiplier(1.0) .setMaxRpcTimeout(Duration.ZERO) .setTotalTimeout(Duration.ofMillis(300000L)) .build())
Both retries and LROs share the same RetrySettings class. Note the corresponding link:
- Total Timeout (Max Time allowed for polling): 5 minutes
- Initial Retry Delay (Initial delay before first poll): 5 seconds
- Max Retry Delay (Maximum delay between each poll): 45 seconds
- Retry Delay Multiplier (Multiplier value to increase the poll delay): 1.5
The RPC Timeout values have no use in LROs and can be omitted or set to the default values(Duration.ZERO for Timeouts or1.0 for the multiplier).
To configure the LRO values, create an OperationTimedPollAlgorithm object and update theRPC's polling algorithm. For example:
ClusterControllerSettings.BuildersettingsBuilder =ClusterControllerSettings.newBuilder();TimedRetryAlgorithmtimedRetryAlgorithm =OperationTimedPollAlgorithm.create(RetrySettings.newBuilder().setInitialRetryDelay(Duration.ofMillis(500L)).setRetryDelayMultiplier(1.5).setMaxRetryDelay(Duration.ofMillis(5000L)).setInitialRpcTimeout(Duration.ZERO)// ignored.setRpcTimeoutMultiplier(1.0)// ignored.setMaxRpcTimeout(Duration.ZERO)// ignored.setTotalTimeout(Duration.ofHours(24L))// set polling timeout to 24 hours.build());settingsBuilder.createClusterOperationSettings().setPollingAlgorithm(timedRetryAlgorithm);ClusterControllerClientclusterControllerClient =ClusterControllerClient.create(settingsBuilder.build());
Note: The configuration aboveonly modifies the LRO values for thecreateClusterOperation RPC.The other RPCs in the Client will still use each RPC's pre-configured LRO values.
If you are using more than one Google Cloud client library, we recommend you use one ofour Bill of Material (BOM) artifacts to help manage dependency versions. For more information,seeUsing the Cloud Client Libraries.
Java 8 or above is required for using the clients in this repository.
Clients in this repository use either HTTP or gRPC for the transport layer. AllHTTP-based clients should work in all environments.
For clients that use gRPC, the supported platforms are constrained by the platformsthatForked 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 withthreading restrictions.
Thus, the following are not supported:
- Android
- ConsiderFirebase, which includes many of these APIs.
- It is possible to use these libraries in many cases, although it is unsupported.You can find examples, such asthis one,in thisexample repository but consider the risks carefully before using these libraries in an application.
- 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)
- Alpine Linux (Java 11+)
This library provides tools to help write tests for code that uses google-cloud services.
SeeTESTING to read more about using our testing helpers.
This library followsSemantic Versioning, with someadditional qualifications:
Components marked with
@BetaApior@Experimentalare considered to be "0.x"features inside a "1.x" library. This means they can change between minor andpatch releases in incompatible ways. These features should not be used by anylibrary "B" that itself has consumers, unless the components of library B thatuse@BetaApifeatures are also marked with@BetaApi. Features marked as@BetaApiare on a path to eventually become "1.x" features with the markerremoved.Special exception for google-cloud-java: google-cloud-java isallowed to depend on
@BetaApifeatures in gax-java without declaring the consumingcode@BetaApi, because gax-java and google-cloud-java move in stepwith each other. For this reason, gax-java should not be usedindependently of google-cloud-java.Components marked with
@InternalApiare technically public, but onlybecause of the limitations of Java's accessmodifiers. For the purposes of semver, they should be considered private.Interfaces marked with
@InternalExtensionOnlyare public, but should only beimplemented by internal classes. For the purposes of semver, we reserve the rightto add to these interfaces without default implementations (for Java 7).
Please note these clients are currently under active development. Any release versioned 0.x.y issubject to backwards incompatible changes at any time.
Libraries defined at a Stable quality level are expected to be stable and all updates in thelibraries are guaranteed to be backwards-compatible. Any backwards-incompatible changes will leadto the major version increment (1.x.y -> 2.0.0).
Libraries defined at a Preview quality level are still a work-in-progress andare more likely to get backwards-incompatible updates. Additionally, it's possible for Previewlibraries to get deprecated and deleted before ever being promoted to Preview or Stable.
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 accountkey 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 7runtime. However, Datastore, Storage, and Bigquery should work.
Contributions to this library are always welcome and highly encouraged.
Seegoogle-cloud'sCONTRIBUTING documentation and theshared documentation for more information on how to get started.
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. SeeCode of Conduct for more information.
Apache 2.0 - SeeLICENSE for more information.
About
Google Cloud Client Library for Java
Topics
Resources
License
Code of conduct
Contributing
Security policy
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.