23
23
import com .google .gcloud .spi .StorageRpc .Tuple ;
24
24
import com .google .gcloud .storage .Blob ;
25
25
import com .google .gcloud .storage .BlobId ;
26
- import com .google .gcloud .storage .BlobInfo ;
27
26
import com .google .gcloud .storage .Bucket ;
28
- import com .google .gcloud .storage .BucketInfo ;
29
27
import com .google .gcloud .storage .CopyWriter ;
30
28
import com .google .gcloud .storage .Storage ;
31
29
import com .google .gcloud .storage .Storage .ComposeRequest ;
@@ -93,7 +91,7 @@ private abstract static class StorageAction<T> {
93
91
94
92
abstract void run (Storage storage , T request ) throws Exception ;
95
93
96
- abstract T parse (String ... args ) throws Exception ;
94
+ abstract T parse (Storage storage , String ... args ) throws Exception ;
97
95
98
96
protected String params () {
99
97
return "" ;
@@ -103,7 +101,7 @@ protected String params() {
103
101
private abstract static class BlobsAction extends StorageAction <BlobId []> {
104
102
105
103
@ Override
106
- BlobId [] parse (String ... args ) {
104
+ BlobId [] parse (Storage storage , String ... args ) {
107
105
if (args .length < 2 ) {
108
106
throw new IllegalArgumentException ();
109
107
}
@@ -138,33 +136,33 @@ public void run(Storage storage, BlobId... blobIds) {
138
136
System .out .println ("No such bucket" );
139
137
return ;
140
138
}
141
- System .out .println ("Bucket info: " + bucket . info () );
139
+ System .out .println ("Bucket info: " + bucket );
142
140
} else {
143
141
// get Blob
144
142
Blob blob = Blob .get (storage , blobIds [0 ]);
145
143
if (blob == null ) {
146
144
System .out .println ("No such object" );
147
145
return ;
148
146
}
149
- System .out .println ("Blob info: " + blob . info () );
147
+ System .out .println ("Blob info: " + blob );
150
148
}
151
149
} else {
152
150
// use batch to get multiple blobs.
153
151
List <Blob > blobs = Blob .get (storage , Arrays .asList (blobIds ));
154
152
for (Blob blob : blobs ) {
155
153
if (blob != null ) {
156
- System .out .println (blob . info () );
154
+ System .out .println (blob );
157
155
}
158
156
}
159
157
}
160
158
}
161
159
162
160
@ Override
163
- BlobId [] parse (String ... args ) {
161
+ BlobId [] parse (Storage storage , String ... args ) {
164
162
if (args .length < 2 ) {
165
163
return new BlobId [] {BlobId .of (args [0 ], "" )};
166
164
}
167
- return super .parse (args );
165
+ return super .parse (storage , args );
168
166
}
169
167
170
168
@ Override
@@ -204,7 +202,7 @@ public void run(Storage storage, BlobId... blobIds) {
204
202
private static class ListAction extends StorageAction <String > {
205
203
206
204
@ Override
207
- String parse (String ... args ) {
205
+ String parse (Storage storage , String ... args ) {
208
206
if (args .length == 0 ) {
209
207
return null ;
210
208
}
@@ -218,9 +216,9 @@ String parse(String... args) {
218
216
public void run (Storage storage , String bucketName ) {
219
217
if (bucketName == null ) {
220
218
// list buckets
221
- Iterator <BucketInfo > bucketInfoIterator = storage .list ().iterateAll ();
222
- while (bucketInfoIterator .hasNext ()) {
223
- System .out .println (bucketInfoIterator .next ());
219
+ Iterator <Bucket > bucketIterator = storage .list ().iterateAll ();
220
+ while (bucketIterator .hasNext ()) {
221
+ System .out .println (bucketIterator .next ());
224
222
}
225
223
} else {
226
224
// list a bucket's blobs
@@ -231,7 +229,7 @@ public void run(Storage storage, String bucketName) {
231
229
}
232
230
Iterator <Blob > blobIterator = bucket .list ().iterateAll ();
233
231
while (blobIterator .hasNext ()) {
234
- System .out .println (blobIterator .next (). info () );
232
+ System .out .println (blobIterator .next ());
235
233
}
236
234
}
237
235
}
@@ -247,17 +245,16 @@ public String params() {
247
245
*
248
246
* @see <a href="https://cloud.google.com/storage/docs/json_api/v1/objects/insert">Objects: insert</a>
249
247
*/
250
- private static class UploadAction extends StorageAction <Tuple <Path , BlobInfo >> {
248
+ private static class UploadAction extends StorageAction <Tuple <Path , Blob >> {
251
249
@ Override
252
- public void run (Storage storage , Tuple <Path , BlobInfo > tuple ) throws Exception {
250
+ public void run (Storage storage , Tuple <Path , Blob > tuple ) throws Exception {
253
251
run (storage , tuple .x (), tuple .y ());
254
252
}
255
253
256
- private void run (Storage storage , Path uploadFrom , BlobInfo blobInfo ) throws IOException {
254
+ private void run (Storage storage , Path uploadFrom , Blob blob ) throws IOException {
257
255
if (Files .size (uploadFrom ) > 1_000_000 ) {
258
256
// When content is not available or large (1MB or more) it is recommended
259
257
// to write it in chunks via the blob's channel writer.
260
- Blob blob = new Blob (storage , blobInfo );
261
258
try (WriteChannel writer = blob .writer ()) {
262
259
byte [] buffer = new byte [1024 ];
263
260
try (InputStream input = Files .newInputStream (uploadFrom )) {
@@ -274,20 +271,20 @@ private void run(Storage storage, Path uploadFrom, BlobInfo blobInfo) throws IOE
274
271
} else {
275
272
byte [] bytes = Files .readAllBytes (uploadFrom );
276
273
// create the blob in one request.
277
- storage .create (blobInfo , bytes );
274
+ storage .create (blob , bytes );
278
275
}
279
276
System .out .println ("Blob was created" );
280
277
}
281
278
282
279
@ Override
283
- Tuple <Path , BlobInfo > parse (String ... args ) throws IOException {
280
+ Tuple <Path , Blob > parse (Storage storage , String ... args ) throws IOException {
284
281
if (args .length < 2 || args .length > 3 ) {
285
282
throw new IllegalArgumentException ();
286
283
}
287
284
Path path = Paths .get (args [0 ]);
288
285
String contentType = Files .probeContentType (path );
289
286
String blob = args .length < 3 ? path .getFileName ().toString () : args [2 ];
290
- return Tuple .of (path , BlobInfo .builder (args [1 ], blob ).contentType (contentType ).build ());
287
+ return Tuple .of (path , Blob .builder (storage , args [1 ], blob ).contentType (contentType ).build ());
291
288
}
292
289
293
290
@ Override
@@ -320,7 +317,7 @@ private void run(Storage storage, BlobId blobId, Path downloadTo) throws IOExcep
320
317
if (downloadTo != null ) {
321
318
writeTo = new PrintStream (new FileOutputStream (downloadTo .toFile ()));
322
319
}
323
- if (blob .info (). size () < 1_000_000 ) {
320
+ if (blob .size () < 1_000_000 ) {
324
321
// Blob is small read all its content in one request
325
322
byte [] content = blob .content ();
326
323
writeTo .write (content );
@@ -344,7 +341,7 @@ private void run(Storage storage, BlobId blobId, Path downloadTo) throws IOExcep
344
341
}
345
342
346
343
@ Override
347
- Tuple <BlobId , Path > parse (String ... args ) {
344
+ Tuple <BlobId , Path > parse (Storage storage , String ... args ) {
348
345
if (args .length < 2 || args .length > 3 ) {
349
346
throw new IllegalArgumentException ();
350
347
}
@@ -379,11 +376,11 @@ public void run(Storage storage, CopyRequest request) {
379
376
}
380
377
381
378
@ Override
382
- CopyRequest parse (String ... args ) {
379
+ CopyRequest parse (Storage storage , String ... args ) {
383
380
if (args .length != 4 ) {
384
381
throw new IllegalArgumentException ();
385
382
}
386
- return CopyRequest .of (args [0 ], args [1 ], BlobId .of (args [2 ], args [3 ]));
383
+ return CopyRequest .of (storage , args [0 ], args [1 ], BlobId .of (args [2 ], args [3 ]));
387
384
}
388
385
389
386
@ Override
@@ -400,17 +397,17 @@ public String params() {
400
397
private static class ComposeAction extends StorageAction <ComposeRequest > {
401
398
@ Override
402
399
public void run (Storage storage , ComposeRequest request ) {
403
- BlobInfo composedBlobInfo = storage .compose (request );
404
- System .out .println ("Composed " + composedBlobInfo );
400
+ Blob composedBlob = storage .compose (request );
401
+ System .out .println ("Composed " + composedBlob );
405
402
}
406
403
407
404
@ Override
408
- ComposeRequest parse (String ... args ) {
405
+ ComposeRequest parse (Storage storage , String ... args ) {
409
406
if (args .length < 3 ) {
410
407
throw new IllegalArgumentException ();
411
408
}
412
409
ComposeRequest .Builder request = ComposeRequest .builder ();
413
- request .target (BlobInfo .builder (args [0 ], args [args .length - 1 ]).build ());
410
+ request .target (Blob .builder (storage , args [0 ], args [args .length - 1 ]).build ());
414
411
for (int i = 1 ; i < args .length - 1 ; i ++) {
415
412
request .addSource (args [i ]);
416
413
}
@@ -443,12 +440,12 @@ private void run(Storage storage, BlobId blobId, Map<String, String> metadata) {
443
440
System .out .println ("No such object" );
444
441
return ;
445
442
}
446
- Blob updateBlob = blob .update (blob .info (). toBuilder ().metadata (metadata ).build ());
447
- System .out .println ("Updated " + updateBlob . info () );
443
+ Blob updateBlob = blob .update (blob .toBuilder ().metadata (metadata ).build ());
444
+ System .out .println ("Updated " + updateBlob );
448
445
}
449
446
450
447
@ Override
451
- Tuple <BlobId , Map <String , String >> parse (String ... args ) {
448
+ Tuple <BlobId , Map <String , String >> parse (Storage storage , String ... args ) {
452
449
if (args .length < 2 ) {
453
450
throw new IllegalArgumentException ();
454
451
}
@@ -478,25 +475,25 @@ public String params() {
478
475
* @see <a href="https://cloud.google.com/storage/docs/access-control#Signed-URLs">Signed URLs</a>
479
476
*/
480
477
private static class SignUrlAction extends
481
- StorageAction <Tuple <ServiceAccountAuthCredentials , BlobInfo >> {
478
+ StorageAction <Tuple <ServiceAccountAuthCredentials , Blob >> {
482
479
483
480
private static final char [] PASSWORD = "notasecret" .toCharArray ();
484
481
485
482
@ Override
486
- public void run (Storage storage , Tuple <ServiceAccountAuthCredentials , BlobInfo > tuple )
483
+ public void run (Storage storage , Tuple <ServiceAccountAuthCredentials , Blob > tuple )
487
484
throws Exception {
488
485
run (storage , tuple .x (), tuple .y ());
489
486
}
490
487
491
- private void run (Storage storage , ServiceAccountAuthCredentials cred , BlobInfo blobInfo )
488
+ private void run (Storage storage , ServiceAccountAuthCredentials cred , Blob blob )
492
489
throws IOException {
493
- Blob blob = new Blob (storage , blobInfo );
494
490
System .out .println ("Signed URL: "
495
491
+ blob .signUrl (1 , TimeUnit .DAYS , SignUrlOption .serviceAccount (cred )));
496
492
}
497
493
498
494
@ Override
499
- Tuple <ServiceAccountAuthCredentials , BlobInfo > parse (String ... args ) throws IOException ,
495
+ Tuple <ServiceAccountAuthCredentials , Blob > parse (Storage storage , String ... args )
496
+ throws IOException ,
500
497
KeyStoreException , CertificateException , NoSuchAlgorithmException ,
501
498
UnrecoverableKeyException {
502
499
if (args .length != 4 ) {
@@ -506,7 +503,7 @@ Tuple<ServiceAccountAuthCredentials, BlobInfo> parse(String... args) throws IOEx
506
503
keystore .load (Files .newInputStream (Paths .get (args [0 ])), PASSWORD );
507
504
PrivateKey privateKey = (PrivateKey ) keystore .getKey ("privatekey" , PASSWORD );
508
505
ServiceAccountAuthCredentials cred = AuthCredentials .createFor (args [1 ], privateKey );
509
- return Tuple .of (cred , BlobInfo .builder (BlobId .of (args [2 ], args [3 ])).build ());
506
+ return Tuple .of (cred , Blob .builder (storage , BlobId .of (args [2 ], args [3 ])).build ());
510
507
}
511
508
512
509
@ Override
@@ -569,7 +566,7 @@ public static void main(String... args) throws Exception {
569
566
Storage storage = optionsBuilder .build ().service ();
570
567
Object request ;
571
568
try {
572
- request = action .parse (args );
569
+ request = action .parse (storage , args );
573
570
} catch (IllegalArgumentException ex ) {
574
571
System .out .println ("Invalid input for action '" + actionName + "'" );
575
572
System .out .println ("Expected: " + action .params ());
0 commit comments