1616
1717package com .google .gcloud .examples ;
1818
19- import com .google .gcloud .storage .Blob ;
20- import com .google .gcloud .storage .Bucket ;
21- import com .google .gcloud .storage .ListOptions ;
22- import com .google .gcloud .storage .StorageService ;
23- import com .google .gcloud .storage .StorageServiceFactory ;
24- import com .google .gcloud .storage .StorageServiceOptions ;
19+ import com .google .gcloud .storage .*;
2520
21+ import java .nio .file .Files ;
22+ import java .nio .file .Path ;
23+ import java .nio .file .Paths ;
2624import java .util .Arrays ;
2725import java .util .HashMap ;
2826import java .util .Iterator ;
3836 * <li>compile using maven - {@code mvn compile}</li>
3937 * <li>run using maven - {@code mvn exec:java
4038 * -Dexec.mainClass="com.google.gcloud.examples.StorageExample"
41- * -Dexec.args="list [<bucket>]|info [<bucket> [<file>]]|get <bucket> <path>|upload <local_file> <bucket> [<path>]|delete <bucket> <path>"}</li>
39+ * -Dexec.args="project_id list [<bucket>]|info [<bucket> [<file>]]|get <bucket> <path>|upload <local_file> <bucket> [<path>]|delete <bucket> <path>"}</li>
4240 * </ol>
4341 */
4442public class StorageExample {
@@ -47,15 +45,39 @@ public class StorageExample {
4745
4846 private static abstract class StorageAction <T > {
4947
50- abstract void run (StorageService storage , T request );
48+ abstract void run (StorageService storage , T request ) throws Exception ;
5149
52- abstract T parse (String ... args );
50+ abstract T parse (String ... args ) throws IllegalArgumentException ;
5351
5452 protected String params () {
5553 return "" ;
5654 }
5755 }
5856
57+ private static class Tuple <X , Y > {
58+
59+ private final X x ;
60+ private final Y y ;
61+
62+ private Tuple (X x , Y y ) {
63+ this .x = x ;
64+ this .y = y ;
65+ }
66+
67+ static <X , Y > Tuple <X , Y > of (X x , Y y ) {
68+ return new Tuple <>(x , y );
69+ }
70+
71+ X first () {
72+ return x ;
73+ }
74+
75+ Y second () {
76+ return y ;
77+ }
78+ }
79+
80+
5981 private static abstract class BlobAction extends StorageAction <Blob > {
6082
6183 @ Override
@@ -142,11 +164,39 @@ public String params() {
142164 }
143165 }
144166
167+ private static class UploadAction extends StorageAction <Tuple <Path , Blob >> {
168+ @ Override
169+ public void run (StorageService storage , Tuple <Path , Blob > tuple ) throws Exception {
170+ if (Files .size (tuple .first ()) > 1_000_000 ) {
171+ // todo upload via streaming API
172+ throw new IllegalArgumentException ("file is too big" );
173+ } else {
174+ byte [] bytes = Files .readAllBytes (tuple .first ());
175+ System .out .println (storage .create (tuple .second (), bytes ));
176+ }
177+ }
178+
179+ @ Override
180+ Tuple <Path , Blob > parse (String ... args ) {
181+ if (args .length < 2 || args .length > 3 ) {
182+ throw new IllegalArgumentException ();
183+ }
184+ Path path = Paths .get (args [0 ]);
185+ String blob = args .length < 3 ? path .getFileName ().toString () : args [2 ];
186+ return Tuple .of (path , Blob .of (args [1 ], blob ));
187+ }
188+
189+ @ Override
190+ public String params () {
191+ return "<local_file> <bucket> [<path>]" ;
192+ }
193+ }
194+
145195 static {
146196 ACTIONS .put ("info" , new InfoAction ());
147197 ACTIONS .put ("delete" , new DeleteAction ());
148198 ACTIONS .put ("list" , new ListAction ());
149- // ACTIONS.put("upload", new UploadAction());
199+ ACTIONS .put ("upload" , new UploadAction ());
150200 // todo: implement get
151201 }
152202
@@ -165,26 +215,29 @@ public static void printUsage() {
165215 System .out .printf ("Usage: %s [%s]%n" ,
166216 StorageExample .class .getSimpleName (), actionAndParams );
167217 }
168- public static void main (String ... args ) {
169- if (args .length == 0 ) {
170- System .out .println ("Missing required action" );
218+
219+ @ SuppressWarnings ("unchecked" )
220+ public static void main (String ... args ) throws Exception {
221+ if (args .length < 2 ) {
222+ System .out .println ("Missing required project id and action" );
171223 printUsage ();
172224 return ;
173225 }
174- StorageAction action = ACTIONS .get (args [0 ]);
226+ String projectId = args [0 ];
227+ StorageAction action = ACTIONS .get (args [1 ]);
175228 if (action == null ) {
176- System .out .println ("Unrecognized action '" + args [0 ] + "'" );
229+ System .out .println ("Unrecognized action '" + args [1 ] + "'" );
177230 printUsage ();
178231 return ;
179232 }
180- StorageService storage =
181- StorageServiceFactory .instance ().get (StorageServiceOptions . builder (). build () );
182- args = args .length > 1 ? Arrays .copyOfRange (args , 1 , args .length ): new String []{};
233+ StorageServiceOptions options = StorageServiceOptions . builder (). project ( args [ 0 ]). build ();
234+ StorageService storage = StorageServiceFactory .instance ().get (options );
235+ args = args .length > 2 ? Arrays .copyOfRange (args , 2 , args .length ): new String []{};
183236 Object request ;
184237 try {
185238 request = action .parse (args );
186239 } catch (Exception ex ) {
187- System .out .println ("Invalid input for action '" + args [0 ] + "'" );
240+ System .out .println ("Invalid input for action '" + args [1 ] + "'" );
188241 System .out .println ("Expected: " + action .params ());
189242 return ;
190243 }
0 commit comments