Skip to content

Commit 55a7855

Browse files
author
Ajay Kannan
committed
Add project ID section
1 parent fe57e98 commit 55a7855

File tree

1 file changed

+35
-9
lines changed

1 file changed

+35
-9
lines changed

README.md

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,36 @@ Example Applications
3737
- [`StorageExample`](https://github.com/GoogleCloudPlatform/gcloud-java/blob/master/gcloud-java-examples/src/main/java/com/google/gcloud/examples/StorageExample.java) - A simple command line interface providing some of Cloud Storage's functionality
3838
- Read more about using this application on the [`gcloud-java-examples` docs page](http://googlecloudplatform.github.io/gcloud-java/apidocs/?com/google/gcloud/examples/StorageExample.html).
3939

40+
Specifying a Project
41+
--------------------
42+
43+
Most `gcloud-java` libraries require a project ID. There are multiple ways to specify this project ID.
44+
45+
1. When using `gcloud-java` libraries from within Compute/App Engine, there's no need to specify a project ID. It is automatically inferred from the production environment.
46+
2. When using `gcloud-java` elsewhere, you can do one of the following:
47+
* Supply the project ID when building the service options. For example, to use Datastore from a project with ID "PROJECT_ID", you can write:
48+
49+
```java
50+
Datastore datastore = DatastoreOptions.builder().projectId("PROJECT_ID").build().service();
51+
```
52+
* Specify the environment variable `GCLOUD_PROJECT`. For example, type the following into command line:
53+
54+
```bash
55+
export GCLOUD_PROJECT=PROJECT_ID
56+
```
57+
* 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:
58+
59+
```
60+
gcloud config set project PROJECT_ID
61+
```
62+
63+
`gcloud-java` determines the project ID from the following sources in the listed order, stopping once it finds a value:
64+
65+
1. Project ID supplied when building the service options.
66+
2. Project ID specified by the environment variable `GCLOUD_PROJECT`.
67+
3. Project ID used by App Engine.
68+
4. Project ID specified in the Google Cloud SDK.
69+
4070
Authentication
4171
--------------
4272
@@ -45,15 +75,18 @@ There are multiple ways to authenticate to use Google Cloud services.
4575
1. When using `gcloud-java` libraries from within Compute/App Engine, no additional authentication steps are necessary.
4676
2. When using `gcloud-java` libraries elsewhere, there are two options:
4777
* [Generate a JSON service account key](https://cloud.google.com/storage/docs/authentication?hl=en#service_accounts). After downloading that key, you must do one of the following:
48-
* 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`
78+
* Define the environment variable GOOGLE_APPLICATION_CREDENTIALS to be the location of the key. For example:
79+
```bash
80+
export GOOGLE_APPLICATION_CREDENTIALS=/path/to/my/key.json
81+
```
4982
* 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:
5083
```java
5184
Storage storage = StorageOptions.builder()
5285
.authCredentials(AuthCredentials.createForJson(new FileInputStream("/path/to/my/key.json"))
5386
.build()
5487
.service();
5588
```
56-
* If running locally for development/testing, you can use use [Google Cloud SDK](https://cloud.google.com/sdk/?hl=en). To use the SDK authentication, [download the SDK](https://cloud.google.com/sdk/?hl=en) if you haven't already. Then login using the SDK (`gcloud auth login` in command line), and set your current project using `gcloud config set project PROJECT_ID`.
89+
* If running locally for development/testing, you can use use Google Cloud SDK. Download the SDK if you haven't already, then login using the SDK (`gcloud auth login` in command line). Be sure your current project is set correctly by running `gcloud config set project PROJECT_ID`.
5790
5891
`gcloud-java` looks for credentials in the following order, stopping once it finds credentials:
5992
@@ -63,13 +96,6 @@ There are multiple ways to authenticate to use Google Cloud services.
6396
4. Google Cloud SDK credentials
6497
5. Compute Engine credentials
6598
66-
Note that this sequence is different than the order in which `gcloud-java` determines the project ID. The project ID is determined in the following order:
67-
68-
1. Project ID supplied when building the service options
69-
2. Project ID specified by the environment variable `GCLOUD_PROJECT`
70-
3. App Engine project ID
71-
4. Google Cloud SDK project ID
72-
7399
Google Cloud Datastore
74100
----------------------
75101

0 commit comments

Comments
 (0)