|
4 | 4 |
|
5 | 5 | * [Java on Google Cloud][cloud-java] |
6 | 6 |
|
| 7 | +## Documentation |
| 8 | + |
| 9 | +See the [official guide](https://cloud.google.com/java/docs/setup) to get setup and started with development. |
| 10 | + |
7 | 11 | ## Supported APIs |
8 | 12 |
|
9 | 13 | Libraries are available on GitHub and Maven Central for developing Java applications that interact with individual Google Cloud services: |
@@ -236,371 +240,6 @@ If the service is not listed, [google-api-java-client][google-api-java-client-se |
236 | 240 |
|
237 | 241 | *When building Java applications, preference should be given to the libraries listed in the table.* |
238 | 242 |
|
239 | | - |
240 | | - |
241 | | -## Specifying a Project ID |
242 | | - |
243 | | -Most `google-cloud` libraries require a project ID. There are multiple ways to specify this project ID. |
244 | | - |
245 | | -1. When using `google-cloud` libraries from within Compute/App Engine, there's no need to specify a project ID. It is automatically inferred from the production environment. |
246 | | -2. When using `google-cloud` elsewhere, you can do one of the following: |
247 | | -* Supply the project ID when building the service options. For example, to use Datastore from a project with ID "PROJECT_ID", you can write: |
248 | | - |
249 | | - ```java |
250 | | - Datastore datastore = DatastoreOptions.newBuilder().setProjectId("PROJECT_ID").build().getService(); |
251 | | - ``` |
252 | | -* Specify the environment variable `GOOGLE_CLOUD_PROJECT` to be your desired project ID. |
253 | | -* Set the project ID using the [Google Cloud SDK](https://cloud.google.com/sdk/?hl=en). To use the SDK, [download the SDK](https://cloud.google.com/sdk/?hl=en) if you haven't already, and set the project ID from the command line. For example: |
254 | | - |
255 | | - ``` |
256 | | - gcloud config set project PROJECT_ID |
257 | | - ``` |
258 | | - |
259 | | -`google-cloud` determines the project ID from the following sources in the listed order, stopping once it finds a value: |
260 | | - |
261 | | -1. The project ID supplied when building the service options |
262 | | -2. Project ID specified by the environment variable `GOOGLE_CLOUD_PROJECT` |
263 | | -3. The App Engine / Compute Engine project ID |
264 | | -4. The project ID specified in the JSON credentials file pointed by the `GOOGLE_APPLICATION_CREDENTIALS` environment variable |
265 | | -5. The Google Cloud SDK project ID |
266 | | - |
267 | | -In cases where the library may expect a project ID explicitly, we provide a helper that can provide the inferred project ID: |
268 | | - ```java |
269 | | - import com.google.cloud.ServiceOptions; |
270 | | - ... |
271 | | - String projectId = ServiceOptions.getDefaultProjectId(); |
272 | | - ``` |
273 | | - |
274 | | -## Authentication |
275 | | - |
276 | | -`google-cloud-java` uses |
277 | | -[https://github.com/googleapis/google-auth-library-java](https://github.com/googleapis/google-auth-library-java) |
278 | | -to authenticate requests. `google-auth-library-java` supports a wide range of authentication types; |
279 | | -see the project's [README](https://github.com/google/google-auth-library-java/blob/main/README.md) |
280 | | -and [javadoc](https://cloud.google.com/java/docs/reference/google-auth-library/latest/overview) for more |
281 | | -details. |
282 | | - |
283 | | -### Google Cloud Platform environment |
284 | | - |
285 | | -When using Google Cloud libraries from a Google Cloud Platform environment such as Compute Engine, |
286 | | -Kubernetes Engine, or App Engine, no additional authentication steps are necessary. |
287 | | - |
288 | | -For example: |
289 | | - |
290 | | -```java |
291 | | -Storage storage = StorageOptions.getDefaultInstance().getService(); |
292 | | -``` |
293 | | - |
294 | | -or: |
295 | | - |
296 | | -```java |
297 | | -CloudTasksClient cloudTasksClient = CloudTasksClient.create(); |
298 | | -``` |
299 | | - |
300 | | -### Other environments |
301 | | - |
302 | | -#### Using a service account (recommended) |
303 | | - |
304 | | -1. [Generate a JSON service account key](https://cloud.google.com/storage/docs/authentication?hl=en#service_accounts). |
305 | | - |
306 | | -2. After downloading that key, you must do one of the following: |
307 | | - * Define the environment variable GOOGLE_APPLICATION_CREDENTIALS to be the location of the key. |
308 | | - For example: |
309 | | - ```bash |
310 | | - export GOOGLE_APPLICATION_CREDENTIALS=/path/to/my/key.json |
311 | | - ``` |
312 | | - * Supply the JSON credentials file when building the service options. For example, this Storage |
313 | | - object has the necessary permissions to interact with your Google Cloud Storage data: |
314 | | - ```java |
315 | | - Storage storage = StorageOptions.newBuilder() |
316 | | - .setCredentials(ServiceAccountCredentials.fromStream(new FileInputStream("/path/to/my/key.json"))) |
317 | | - .build() |
318 | | - .getService(); |
319 | | - ``` |
320 | | - |
321 | | -#### Local development/testing |
322 | | - |
323 | | -If running locally for development/testing, you can use the [Google Cloud SDK](https://cloud.google.com/sdk/). |
324 | | -Create Application Default Credentials with `gcloud auth application-default login`, and then |
325 | | -`google-cloud` will automatically detect such credentials. |
326 | | - |
327 | | -#### Existing OAuth2 access token |
328 | | - |
329 | | -If you already have an OAuth2 access token, you can use it to authenticate (notice that in this case, the |
330 | | -access token will not be automatically refreshed): |
331 | | - |
332 | | -```java |
333 | | -Credentials credentials = GoogleCredentials.create(new AccessToken(accessToken, expirationTime)); |
334 | | -Storage storage = StorageOptions.newBuilder() |
335 | | - .setCredentials(credentials) |
336 | | - .build() |
337 | | - .getService(); |
338 | | -``` |
339 | | - |
340 | | -or: |
341 | | - |
342 | | -```java |
343 | | -Credentials credentials = GoogleCredentials.create(new AccessToken(accessToken, expirationTime)); |
344 | | -CloudTasksSettings cloudTasksSettings = CloudTasksSettings.newBuilder() |
345 | | - .setCredentialProvider(FixedCredentialsProvider.create(credentials)) |
346 | | - .build(); |
347 | | -CloudTasksClient cloudTasksClient = CloudTasksClient.create(cloudTasksSettings); |
348 | | -``` |
349 | | - |
350 | | -### Application Default Credentials |
351 | | - |
352 | | -If no credentials are provided, `google-cloud` will attempt to detect them from the environment |
353 | | -using `GoogleCredentials.getApplicationDefault()` which will search for Application Default |
354 | | -Credentials in the following locations (in order): |
355 | | -
|
356 | | -1. The credentials file pointed to by the `GOOGLE_APPLICATION_CREDENTIALS` environment variable |
357 | | -2. Credentials provided by the Google Cloud SDK `gcloud auth application-default login` command |
358 | | -3. Google App Engine built-in credentials |
359 | | -4. Google Cloud Shell built-in credentials |
360 | | -5. Google Compute Engine built-in credentials |
361 | | -
|
362 | | -### Authenticating with an API Key |
363 | | -
|
364 | | -[Authenticating with API Keys](https://cloud.google.com/docs/authentication/api-keys) is supported by a handful of Google Cloud APIs. |
365 | | -
|
366 | | -We are actively exploring ways to improve the API Key experience. |
367 | | -Currently, to use an API Key with a Java client library, you need to set the header for the relevant service Client manually. |
368 | | -
|
369 | | -For example, to set the API Key with the [Language service](https://cloud.google.com/java/docs/reference/google-cloud-language/latest/overview): |
370 | | -
|
371 | | -```java |
372 | | -public LanguageServiceClient createGrpcClientWithApiKey(String apiKey) throws Exception { |
373 | | - // Manually set the api key via the header |
374 | | - Map<String, String> header = new HashMap<String, String>() { {put("x-goog-api-key", apiKey);}}; |
375 | | - FixedHeaderProvider headerProvider = FixedHeaderProvider.create(header); |
376 | | - |
377 | | - // Create the client |
378 | | - TransportChannelProvider transportChannelProvider = InstantiatingGrpcChannelProvider.newBuilder().setHeaderProvider(headerProvider).build(); |
379 | | - LanguageServiceSettings settings = LanguageServiceSettings.newBuilder().setTransportChannelProvider(transportChannelProvider).build(); |
380 | | - LanguageServiceClient client = LanguageServiceClient.create(settings); |
381 | | - return client; |
382 | | - } |
383 | | -``` |
384 | | - |
385 | | -An example instantiation with the Language Client using rest: |
386 | | -```java |
387 | | - public LanguageServiceClient createRestClientWithApiKey(String apiKey) throws Exception { |
388 | | - // Manually set the api key header |
389 | | - Map<String, String> header = new HashMap<String, String>() { {put("x-goog-api-key", apiKey);}}; |
390 | | - FixedHeaderProvider headerProvider = FixedHeaderProvider.create(header); |
391 | | - |
392 | | - // Create the client |
393 | | - TransportChannelProvider transportChannelProvider = InstantiatingHttpJsonChannelProvider.newBuilder().setHeaderProvider(headerProvider).build(); |
394 | | - LanguageServiceSettings settings = LanguageServiceSettings.newBuilder().setTransportChannelProvider(transportChannelProvider).build(); |
395 | | - LanguageServiceClient client = LanguageServiceClient.create(settings); |
396 | | - return client; |
397 | | - } |
398 | | -``` |
399 | | - |
400 | | -## Troubleshooting |
401 | | - |
402 | | -To get help, follow the instructions in the [Troubleshooting document](https://github.com/googleapis/google-cloud-java/blob/main/TROUBLESHOOTING.md). |
403 | | - |
404 | | -## Configuring a Proxy |
405 | | - |
406 | | -Google Cloud client libraries use HTTPS and gRPC in underlying communication |
407 | | -with the services. |
408 | | -In both protocols, you can configure a proxy using `https.proxyHost` |
409 | | -and (optional) `https.proxyPort` properties. |
410 | | - |
411 | | -### gRPC Custom Proxy Configuration |
412 | | - |
413 | | -For a more custom proxy with gRPC, you will need supply a `ProxyDetector` to |
414 | | -the `ManagedChannelBuilder`: |
415 | | - |
416 | | -```java |
417 | | -import com.google.api.core.ApiFunction; |
418 | | -import com.google.api.gax.rpc.TransportChannelProvider; |
419 | | -import com.google.cloud.tasks.v2.CloudTasksClient; |
420 | | -import com.google.cloud.tasks.v2.CloudTasksSettings; |
421 | | -import com.google.cloud.tasks.v2.stub.CloudTasksStubSettings; |
422 | | -import io.grpc.HttpConnectProxiedSocketAddress; |
423 | | -import io.grpc.ManagedChannelBuilder; |
424 | | -import io.grpc.ProxiedSocketAddress; |
425 | | -import io.grpc.ProxyDetector; |
426 | | - |
427 | | -import javax.annotation.Nullable; |
428 | | -import java.io.IOException; |
429 | | -import java.net.InetSocketAddress; |
430 | | -import java.net.SocketAddress; |
431 | | - |
432 | | -public CloudTasksClient getService() throws IOException { |
433 | | - TransportChannelProvider transportChannelProvider = |
434 | | - CloudTasksStubSettings.defaultGrpcTransportProviderBuilder() |
435 | | - .setChannelConfigurator( |
436 | | - new ApiFunction<ManagedChannelBuilder, ManagedChannelBuilder>() { |
437 | | - @Override |
438 | | - public ManagedChannelBuilder apply(ManagedChannelBuilder managedChannelBuilder) { |
439 | | - return managedChannelBuilder.proxyDetector( |
440 | | - new ProxyDetector() { |
441 | | - @Nullable |
442 | | - @Override |
443 | | - public ProxiedSocketAddress proxyFor(SocketAddress socketAddress) |
444 | | - throws IOException { |
445 | | - return HttpConnectProxiedSocketAddress.newBuilder() |
446 | | - .setUsername(PROXY_USERNAME) |
447 | | - .setPassword(PROXY_PASSWORD) |
448 | | - .setProxyAddress(new InetSocketAddress(PROXY_HOST, PROXY_PORT)) |
449 | | - .setTargetAddress((InetSocketAddress) socketAddress) |
450 | | - .build(); |
451 | | - } |
452 | | - }); |
453 | | - } |
454 | | - }) |
455 | | - .build(); |
456 | | - CloudTasksSettings cloudTasksSettings = |
457 | | - CloudTasksSettings.newBuilder() |
458 | | - .setTransportChannelProvider(transportChannelProvider) |
459 | | - .build(); |
460 | | - return CloudTasksClient.create(cloudTasksSettings); |
461 | | -} |
462 | | -``` |
463 | | - |
464 | | -## Long Running Operations |
465 | | - |
466 | | -Long running operations (LROs) are often used for API calls that are expected to |
467 | | -take a long time to complete (i.e. provisioning a GCE instance or a Dataflow pipeline). |
468 | | -The initial API call creates an "operation" on the server and returns an Operation ID |
469 | | -to track its progress. LRO RPCs have the suffix `Async` appended to the call name |
470 | | -(i.e. `clusterControllerClient.createClusterAsync()`) |
471 | | - |
472 | | -Our generated clients provide a nice interface for starting the operation and |
473 | | -then waiting for the operation to complete. This is accomplished by returning an |
474 | | -[`OperationFuture`](https://cloud.google.com/java/docs/reference/gax/latest/com.google.api.gax.longrunning.OperationFuture). |
475 | | -When calling `get()` on the `OperationFuture`, the client library will poll the operation to |
476 | | -check the operation's status. |
477 | | - |
478 | | -For example, take a sample `createCluster` Operation in google-cloud-dataproc v4.20.0: |
479 | | -```java |
480 | | -try (ClusterControllerClient clusterControllerClient = ClusterControllerClient.create()) { |
481 | | - CreateClusterRequest request = |
482 | | - CreateClusterRequest.newBuilder() |
483 | | - .setProjectId("{PROJECT_ID}") |
484 | | - .setRegion("{REGION}") |
485 | | - .setCluster(Cluster.newBuilder().build()) |
486 | | - .setRequestId("{REQUEST_ID}") |
487 | | - .setActionOnFailedPrimaryWorkers(FailureAction.forNumber(0)) |
488 | | - .build(); |
489 | | - OperationFuture<Cluster, ClusterOperationMetadata> future = |
490 | | - clusterControllerClient.createClusterOperationCallable().futureCall(request); |
491 | | - // Do something. |
492 | | - Cluster response = future.get(); |
493 | | -} catch (CancellationException e) { |
494 | | - // Exceeded the default RPC timeout without the Operation completing. |
495 | | - // Library is no longer polling for the Operation status. Consider |
496 | | - // increasing the timeout. |
497 | | -} |
498 | | -``` |
499 | | - |
500 | | -### LRO Timeouts |
501 | | -The polling operations have a default timeout that varies from service to service. |
502 | | -The library will throw a `java.util.concurrent.CancellationException` with the message: |
503 | | -`Task was cancelled.` if the timeout exceeds the operation. A `CancellationException` |
504 | | -does not mean that the backend GCP Operation was cancelled. This exception is thrown from the |
505 | | -client library when it has exceeded the total timeout without receiving a successful status from the operation. |
506 | | -Our client libraries respect the configured values set in the OperationTimedPollAlgorithm for each RPC. |
507 | | - |
508 | | -Note: The client library handles the Operation's polling mechanism for you. By default, there is no need |
509 | | -to manually poll the status yourself. |
510 | | - |
511 | | -### Default LRO Values |
512 | | -Each LRO RPC has a set of pre-configured default values. You can find these values by |
513 | | -searching in each Client's `StubSettings`'s class. The default LRO settings are initialized |
514 | | -inside the `initDefaults()` method in the nested Builder class. |
515 | | - |
516 | | -For example, in google-cloud-aiplatform v3.24.0, the default [OperationTimedPollAlgorithm](https://github.com/googleapis/google-cloud-java/blob/9ae786d1acdc7354adf86b78691570668caa293d/java-aiplatform/google-cloud-aiplatform/src/main/java/com/google/cloud/aiplatform/v1/stub/EndpointServiceStubSettings.java#L755-L765) |
517 | | -has these default values: |
518 | | -```java |
519 | | -OperationTimedPollAlgorithm.create( |
520 | | - RetrySettings.newBuilder() |
521 | | - .setInitialRetryDelay(Duration.ofMillis(5000L)) |
522 | | - .setRetryDelayMultiplier(1.5) |
523 | | - .setMaxRetryDelay(Duration.ofMillis(45000L)) |
524 | | - .setInitialRpcTimeout(Duration.ZERO) |
525 | | - .setRpcTimeoutMultiplier(1.0) |
526 | | - .setMaxRpcTimeout(Duration.ZERO) |
527 | | - .setTotalTimeout(Duration.ofMillis(300000L)) |
528 | | - .build()) |
529 | | -``` |
530 | | -Both retries and LROs share the same RetrySettings class. Note the corresponding link: |
531 | | -- Total Timeout (Max Time allowed for polling): 5 minutes |
532 | | -- Initial Retry Delay (Initial delay before first poll): 5 seconds |
533 | | -- Max Retry Delay (Maximum delay between each poll): 45 seconds |
534 | | -- Retry Delay Multiplier (Multiplier value to increase the poll delay): 1.5 |
535 | | - |
536 | | -The RPC Timeout values have no use in LROs and can be omitted or set to the default values |
537 | | -(`Duration.ZERO` for Timeouts or `1.0` for the multiplier). |
538 | | - |
539 | | -### Configuring LRO Timeouts |
540 | | -To configure the LRO values, create an OperationTimedPollAlgorithm object and update the |
541 | | -RPC's polling algorithm. For example: |
542 | | -```java |
543 | | -ClusterControllerSettings.Builder settingsBuilder = ClusterControllerSettings.newBuilder(); |
544 | | -TimedRetryAlgorithm timedRetryAlgorithm = OperationTimedPollAlgorithm.create( |
545 | | - RetrySettings.newBuilder() |
546 | | - .setInitialRetryDelay(Duration.ofMillis(500L)) |
547 | | - .setRetryDelayMultiplier(1.5) |
548 | | - .setMaxRetryDelay(Duration.ofMillis(5000L)) |
549 | | - .setInitialRpcTimeout(Duration.ZERO) // ignored |
550 | | - .setRpcTimeoutMultiplier(1.0) // ignored |
551 | | - .setMaxRpcTimeout(Duration.ZERO) // ignored |
552 | | - .setTotalTimeout(Duration.ofHours(24L)) // set polling timeout to 24 hours |
553 | | - .build()); |
554 | | -settingsBuilder.createClusterOperationSettings() |
555 | | - .setPollingAlgorithm(timedRetryAlgorithm); |
556 | | -ClusterControllerClient clusterControllerClient = ClusterControllerClient.create(settingsBuilder.build()); |
557 | | -``` |
558 | | - |
559 | | -Note: The configuration above *only* modifies the LRO values for the `createClusterOperation` RPC. |
560 | | -The other RPCs in the Client will still use each RPC's pre-configured LRO values. |
561 | | - |
562 | | -## Managing Dependencies |
563 | | - |
564 | | -If you are using more than one Google Cloud client library, we recommend you use one of |
565 | | -our Bill of Material (BOM) artifacts to help manage dependency versions. For more information, |
566 | | -see [Using the Cloud Client Libraries](https://cloud.google.com/java/docs/bom). |
567 | | - |
568 | | -## Java Versions |
569 | | - |
570 | | -Java 8 or above is required for using the clients in this repository. |
571 | | - |
572 | | -## Supported Platforms |
573 | | - |
574 | | -Clients in this repository use either HTTP or gRPC for the transport layer. All |
575 | | -HTTP-based clients should work in all environments. |
576 | | - |
577 | | -For clients that use gRPC, the supported platforms are constrained by the platforms |
578 | | -that [Forked Tomcat Native](http://netty.io/wiki/forked-tomcat-native.html) supports, |
579 | | -which for architectures means only x86_64, and for operating systems means Mac OS X, |
580 | | -Windows, and Linux. Additionally, gRPC constrains the use of platforms with |
581 | | -threading restrictions. |
582 | | - |
583 | | -Thus, the following are not supported: |
584 | | - |
585 | | -- Android |
586 | | - - Consider [Firebase](https://firebase.google.com), which includes many of these APIs. |
587 | | - - It is possible to use these libraries in many cases, although it is unsupported. |
588 | | - You can find examples, such as [this one](https://github.com/GoogleCloudPlatform/android-docs-samples/tree/master/speech/SpeechRecognitionClient), |
589 | | - in this [example repository](https://github.com/GoogleCloudPlatform/android-docs-samples) but consider the risks carefully before using these libraries in an application. |
590 | | -- Raspberry Pi (since it runs on the ARM architecture) |
591 | | -- Google App Engine Standard Java 7 |
592 | | - |
593 | | -The following environments should work (among others): |
594 | | - |
595 | | -- standalone Windows on x86_64 |
596 | | -- standalone Mac OS X on x86_64 |
597 | | -- standalone Linux on x86_64 |
598 | | -- Google Compute Engine (GCE) |
599 | | -- Google Container Engine (GKE) |
600 | | -- Google App Engine Standard Java 8 (GAE Std J8) |
601 | | -- Google App Engine Flex (GAE Flex) |
602 | | -- Alpine Linux (Java 11+) |
603 | | - |
604 | 243 | ## Testing |
605 | 244 |
|
606 | 245 | This library provides tools to help write tests for code that uses google-cloud services. |
|
0 commit comments