Skip to content

Commit 74a39cc

Browse files
committed
Merge pull request #332 from ajkannan/cred-docs
Add more detail to README's authentication section
2 parents 0e32a4b + 67fdcc0 commit 74a39cc

File tree

1 file changed

+48
-2
lines changed

1 file changed

+48
-2
lines changed

README.md

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,61 @@ Example Applications
4545
- [`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
4646
- 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).
4747

48+
Specifying a Project ID
49+
-----------------------
50+
51+
Most `gcloud-java` libraries require a project ID. There are multiple ways to specify this project ID.
52+
53+
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.
54+
2. When using `gcloud-java` elsewhere, you can do one of the following:
55+
* Supply the project ID when building the service options. For example, to use Datastore from a project with ID "PROJECT_ID", you can write:
56+
57+
```java
58+
Datastore datastore = DatastoreOptions.builder().projectId("PROJECT_ID").build().service();
59+
```
60+
* Specify the environment variable `GCLOUD_PROJECT` to be your desired project ID.
61+
* 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:
62+
63+
```
64+
gcloud config set project PROJECT_ID
65+
```
66+
67+
`gcloud-java` determines the project ID from the following sources in the listed order, stopping once it finds a value:
68+
69+
1. Project ID supplied when building the service options
70+
2. Project ID specified by the environment variable `GCLOUD_PROJECT`
71+
3. App Engine project ID
72+
4. Compute Engine project ID
73+
5. Google Cloud SDK project ID
74+
4875
Authentication
4976
--------------
5077

5178
There are multiple ways to authenticate to use Google Cloud services.
5279

5380
1. When using `gcloud-java` libraries from within Compute/App Engine, no additional authentication steps are necessary.
5481
2. When using `gcloud-java` libraries elsewhere, there are two options:
55-
* [Generate a JSON service account key](https://cloud.google.com/storage/docs/authentication?hl=en#service_accounts). Supply a path to the downloaded JSON credentials file when building the options supplied to datastore/storage constructor.
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`.
82+
* [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:
83+
* Define the environment variable GOOGLE_APPLICATION_CREDENTIALS to be the location of the key. For example:
84+
```bash
85+
export GOOGLE_APPLICATION_CREDENTIALS=/path/to/my/key.json
86+
```
87+
* 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:
88+
```java
89+
Storage storage = StorageOptions.builder()
90+
.authCredentials(AuthCredentials.createForJson(new FileInputStream("/path/to/my/key.json"))
91+
.build()
92+
.service();
93+
```
94+
* 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 to set your project ID as described above.
95+
96+
`gcloud-java` looks for credentials in the following order, stopping once it finds credentials:
97+
98+
1. Credentials supplied when building the service options
99+
2. App Engine credentials
100+
3. Key file pointed to by the GOOGLE_APPLICATION_CREDENTIALS environment variable
101+
4. Google Cloud SDK credentials
102+
5. Compute Engine credentials
57103
58104
Google Cloud Datastore
59105
----------------------

0 commit comments

Comments
 (0)