18
18
19
19
import com .google .api .gax .paging .Page ;
20
20
import com .google .auth .oauth2 .ComputeEngineCredentials ;
21
+ import com .google .auth .appengine .AppEngineCredentials ;
21
22
import com .google .auth .oauth2 .GoogleCredentials ;
22
23
import com .google .cloud .storage .Bucket ;
23
24
import com .google .cloud .storage .Storage ;
24
25
import com .google .cloud .storage .StorageOptions ;
25
- import com .google .common .base .Strings ;
26
26
import com .google .common .collect .Lists ;
27
27
28
28
import java .io .FileInputStream ;
@@ -63,7 +63,7 @@ static void authExplicit(String jsonPath) throws IOException {
63
63
// [END auth_cloud_explicit]
64
64
65
65
// [START auth_cloud_explicit_compute_engine]
66
- static void authComputeExplicit () {
66
+ static void authCompute () {
67
67
// Explicitly request service account credentials from the compute engine instance.
68
68
GoogleCredentials credentials = ComputeEngineCredentials .create ();
69
69
Storage storage = StorageOptions .newBuilder ().setCredentials (credentials ).build ().getService ();
@@ -76,6 +76,20 @@ static void authComputeExplicit() {
76
76
}
77
77
// [END auth_cloud_explicit_compute_engine]
78
78
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
+
79
93
public static void main (String [] args ) throws IOException {
80
94
if (args .length == 0 ) {
81
95
authImplicit ();
@@ -90,7 +104,11 @@ public static void main(String[] args) throws IOException {
90
104
return ;
91
105
}
92
106
if ("compute" .equals (args [0 ])) {
93
- authComputeExplicit ();
107
+ authCompute ();
108
+ return ;
109
+ }
110
+ if ("appengine" .equals (args [0 ])) {
111
+ authAppEngineStandard ();
94
112
return ;
95
113
}
96
114
authImplicit ();
0 commit comments