22
22
import com .google .gcloud .spi .StorageRpc .Tuple ;
23
23
import com .google .gcloud .storage .BatchRequest ;
24
24
import com .google .gcloud .storage .BatchResponse ;
25
+ import com .google .gcloud .storage .Blob ;
25
26
import com .google .gcloud .storage .BlobInfo ;
26
27
import com .google .gcloud .storage .BlobReadChannel ;
27
28
import com .google .gcloud .storage .BlobWriteChannel ;
29
+ import com .google .gcloud .storage .Bucket ;
28
30
import com .google .gcloud .storage .BucketInfo ;
29
31
import com .google .gcloud .storage .Storage ;
30
32
import com .google .gcloud .storage .Storage .ComposeRequest ;
@@ -142,12 +144,12 @@ public void run(Storage storage, BlobInfo... blobInfos) {
142
144
if (blobInfos .length == 1 ) {
143
145
if (blobInfos [0 ].name ().isEmpty ()) {
144
146
// get Bucket
145
- BucketInfo bucketInfo = storage . get ( blobInfos [0 ].bucket ());
146
- System .out .println ("Bucket info: " + bucketInfo );
147
+ Bucket bucket = new Bucket ( storage , blobInfos [0 ].bucket ());
148
+ System .out .println ("Bucket info: " + bucket . reload (). info () );
147
149
} else {
148
150
// get Blob
149
- BlobInfo blobInfo = storage . get ( blobInfos [ 0 ]. bucket () , blobInfos [0 ]. name () );
150
- System .out .println ("Blob info: " + blobInfo );
151
+ Blob blob = new Blob ( storage , blobInfos [0 ]);
152
+ System .out .println ("Blob info: " + blob . reload (). info () );
151
153
}
152
154
} else {
153
155
// use batch to get multiple blobs.
@@ -187,7 +189,7 @@ private static class DeleteAction extends BlobsAction {
187
189
@ Override
188
190
public void run (Storage storage , BlobInfo ... blobInfos ) {
189
191
if (blobInfos .length == 1 ) {
190
- boolean wasDeleted = storage . delete ( blobInfos [ 0 ]. bucket () , blobInfos [0 ]. name () );
192
+ boolean wasDeleted = new Blob ( storage , blobInfos [0 ]). delete ( );
191
193
if (wasDeleted ) {
192
194
System .out .println ("Blob " + blobInfos [0 ] + " was deleted" );
193
195
}
@@ -237,8 +239,9 @@ public void run(Storage storage, String bucket) {
237
239
}
238
240
} else {
239
241
// list a bucket's blobs
240
- for (BlobInfo b : storage .list (bucket )) {
241
- System .out .println (b );
242
+ Bucket functionalBucket = new Bucket (storage , bucket );
243
+ for (Blob b : functionalBucket .list ()) {
244
+ System .out .println (b .info ());
242
245
}
243
246
}
244
247
}
@@ -264,7 +267,8 @@ private void run(Storage storage, Path uploadFrom, BlobInfo blobInfo) throws IOE
264
267
if (Files .size (uploadFrom ) > 1_000_000 ) {
265
268
// When content is not available or large (1MB or more) it is recommended
266
269
// to write it in chunks via the blob's channel writer.
267
- try (BlobWriteChannel writer = storage .writer (blobInfo )) {
270
+ Blob blob = new Blob (storage , blobInfo );
271
+ try (BlobWriteChannel writer = blob .writer ()) {
268
272
byte [] buffer = new byte [1024 ];
269
273
try (InputStream input = Files .newInputStream (uploadFrom )) {
270
274
int limit ;
@@ -318,8 +322,9 @@ public void run(Storage storage, Tuple<BlobInfo, Path> tuple) throws IOException
318
322
319
323
private void run (Storage storage , String bucket , String blobName , Path downloadTo )
320
324
throws IOException {
325
+ Blob blob = new Blob (storage , bucket , blobName );
321
326
BlobInfo blobInfo = storage .get (bucket , blobName );
322
- if (blobInfo == null ) {
327
+ if (! blob . exists () ) {
323
328
System .out .println ("No such object" );
324
329
return ;
325
330
}
@@ -329,11 +334,11 @@ private void run(Storage storage, String bucket, String blobName, Path downloadT
329
334
}
330
335
if (blobInfo .size () < 1_000_000 ) {
331
336
// Blob is small read all its content in one request
332
- byte [] content = storage . readAllBytes ( blobInfo . bucket (), blobInfo . name () );
337
+ byte [] content = blob . content ( );
333
338
writeTo .write (content );
334
339
} else {
335
340
// When Blob size is big or unknown use the blob's channel reader.
336
- try (BlobReadChannel reader = storage .reader (blobInfo . bucket (), blobInfo . name () )) {
341
+ try (BlobReadChannel reader = blob .reader ()) {
337
342
WritableByteChannel channel = Channels .newChannel (writeTo );
338
343
ByteBuffer bytes = ByteBuffer .allocate (64 * 1024 );
339
344
while (reader .read (bytes ) > 0 ) {
@@ -435,7 +440,8 @@ public String params() {
435
440
*
436
441
* @see <a href="https://cloud.google.com/storage/docs/json_api/v1/objects/update">Objects: update</a>
437
442
*/
438
- private static class UpdateMetadataAction extends StorageAction <Tuple <BlobInfo , Map <String , String >>> {
443
+ private static class UpdateMetadataAction extends
444
+ StorageAction <Tuple <BlobInfo , Map <String , String >>> {
439
445
440
446
@ Override
441
447
public void run (Storage storage , Tuple <BlobInfo , Map <String , String >> tuple )
@@ -445,13 +451,13 @@ public void run(Storage storage, Tuple<BlobInfo, Map<String, String>> tuple)
445
451
446
452
private void run (Storage storage , String bucket , String blobName ,
447
453
Map <String , String > metadata ) {
448
- BlobInfo blobInfo = storage . get ( bucket , blobName );
449
- if (blobInfo == null ) {
454
+ Blob blob = new Blob ( storage , bucket , blobName ). reload ( );
455
+ if (! blob . exists () ) {
450
456
System .out .println ("No such object" );
451
457
return ;
452
458
}
453
- blobInfo = storage .update (blobInfo .toBuilder ().metadata (metadata ).build ());
454
- System .out .println ("Updated " + blobInfo );
459
+ Blob updateBlob = blob .update (blob . info () .toBuilder ().metadata (metadata ).build ());
460
+ System .out .println ("Updated " + updateBlob . info () );
455
461
}
456
462
457
463
@ Override
@@ -487,7 +493,7 @@ public String params() {
487
493
private static class SignUrlAction extends
488
494
StorageAction <Tuple <ServiceAccountAuthCredentials , BlobInfo >> {
489
495
490
- private static final char [] PASSWORD = "notasecret" .toCharArray ();
496
+ private static final char [] PASSWORD = "notasecret" .toCharArray ();
491
497
492
498
@ Override
493
499
public void run (Storage storage , Tuple <ServiceAccountAuthCredentials , BlobInfo > tuple )
@@ -501,7 +507,7 @@ private void run(Storage storage, ServiceAccountAuthCredentials cred, BlobInfo b
501
507
cal .add (Calendar .DATE , 1 );
502
508
long expiration = cal .getTimeInMillis () / 1000 ;
503
509
System .out .println ("Signed URL: " +
504
- storage .signUrl (blobInfo , expiration , SignUrlOption .serviceAccount (cred )));
510
+ new Blob ( storage , blobInfo ) .signUrl (expiration , SignUrlOption .serviceAccount (cred )));
505
511
}
506
512
507
513
@ Override
0 commit comments