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