Skip to content

Commit ef8c4dd

Browse files
authored
Add AppEngine Standard credentials to auth sample. (#1075)
1 parent fce57ab commit ef8c4dd

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

auth/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ limitations under the License.
4444
<artifactId>google-cloud-storage</artifactId>
4545
<version>1.14.0</version>
4646
</dependency>
47+
<dependency>
48+
<groupId>com.google.auth</groupId>
49+
<artifactId>google-auth-library-appengine</artifactId>
50+
<version>0.9.0</version>
51+
</dependency>
4752
<!-- END dependencies -->
4853
<dependency>
4954
<groupId>commons-io</groupId>

auth/src/main/java/com/google/cloud/auth/samples/AuthExample.java

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@
1818

1919
import com.google.api.gax.paging.Page;
2020
import com.google.auth.oauth2.ComputeEngineCredentials;
21+
import com.google.auth.appengine.AppEngineCredentials;
2122
import com.google.auth.oauth2.GoogleCredentials;
2223
import com.google.cloud.storage.Bucket;
2324
import com.google.cloud.storage.Storage;
2425
import com.google.cloud.storage.StorageOptions;
25-
import com.google.common.base.Strings;
2626
import com.google.common.collect.Lists;
2727

2828
import java.io.FileInputStream;
@@ -63,7 +63,7 @@ static void authExplicit(String jsonPath) throws IOException {
6363
// [END auth_cloud_explicit]
6464

6565
// [START auth_cloud_explicit_compute_engine]
66-
static void authComputeExplicit() {
66+
static void authCompute() {
6767
// Explicitly request service account credentials from the compute engine instance.
6868
GoogleCredentials credentials = ComputeEngineCredentials.create();
6969
Storage storage = StorageOptions.newBuilder().setCredentials(credentials).build().getService();
@@ -76,6 +76,20 @@ static void authComputeExplicit() {
7676
}
7777
// [END auth_cloud_explicit_compute_engine]
7878

79+
// [START auth_cloud_explicit_app_engine]
80+
static void authAppEngineStandard() throws IOException {
81+
// Explicitly request service account credentials from the app engine standard instance.
82+
GoogleCredentials credentials = AppEngineCredentials.getApplicationDefault();
83+
Storage storage = StorageOptions.newBuilder().setCredentials(credentials).build().getService();
84+
85+
System.out.println("Buckets:");
86+
Page<Bucket> buckets = storage.list();
87+
for (Bucket bucket : buckets.iterateAll()) {
88+
System.out.println(bucket.toString());
89+
}
90+
}
91+
// [END auth_cloud_explicit_app_engine]
92+
7993
public static void main(String[] args) throws IOException {
8094
if (args.length == 0) {
8195
authImplicit();
@@ -90,7 +104,11 @@ public static void main(String[] args) throws IOException {
90104
return;
91105
}
92106
if ("compute".equals(args[0])) {
93-
authComputeExplicit();
107+
authCompute();
108+
return;
109+
}
110+
if ("appengine".equals(args[0])) {
111+
authAppEngineStandard();
94112
return;
95113
}
96114
authImplicit();

0 commit comments

Comments
 (0)