Skip to content

Commit fb25804

Browse files
committed
Update examples to properly use load and BlobId
1 parent 13572e7 commit fb25804

1 file changed

Lines changed: 74 additions & 64 deletions

File tree

gcloud-java-examples/src/main/java/com/google/gcloud/examples/StorageExample.java

Lines changed: 74 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,8 @@
2020
import com.google.gcloud.AuthCredentials.ServiceAccountAuthCredentials;
2121
import com.google.gcloud.RetryParams;
2222
import com.google.gcloud.spi.StorageRpc.Tuple;
23-
import com.google.gcloud.storage.BatchRequest;
24-
import com.google.gcloud.storage.BatchResponse;
2523
import com.google.gcloud.storage.Blob;
24+
import com.google.gcloud.storage.BlobId;
2625
import com.google.gcloud.storage.BlobInfo;
2726
import com.google.gcloud.storage.BlobReadChannel;
2827
import com.google.gcloud.storage.BlobWriteChannel;
@@ -53,6 +52,7 @@
5352
import java.security.cert.CertificateException;
5453
import java.util.Arrays;
5554
import java.util.HashMap;
55+
import java.util.List;
5656
import java.util.Map;
5757
import java.util.concurrent.TimeUnit;
5858

@@ -88,23 +88,23 @@ private static abstract class StorageAction<T> {
8888

8989
abstract void run(Storage storage, T request) throws Exception;
9090

91-
abstract T parse(Storage storage, String... args) throws Exception;
91+
abstract T parse(String... args) throws Exception;
9292

9393
protected String params() {
9494
return "";
9595
}
9696
}
9797

98-
private static abstract class BlobsAction extends StorageAction<Blob[]> {
98+
private static abstract class BlobsAction extends StorageAction<BlobId[]> {
9999

100100
@Override
101-
Blob[] parse(Storage storage, String... args) {
101+
BlobId[] parse(String... args) {
102102
if (args.length < 2) {
103103
throw new IllegalArgumentException();
104104
}
105-
Blob[] blobs = new Blob[args.length - 1];
105+
BlobId[] blobs = new BlobId[args.length - 1];
106106
for (int i = 1; i < args.length; i++) {
107-
blobs[i - 1] = Blob.load(storage, args[0], args[i]);
107+
blobs[i - 1] = BlobId.of(args[0], args[i]);
108108
}
109109
return blobs;
110110
}
@@ -124,35 +124,42 @@ public String params() {
124124
*/
125125
private static class InfoAction extends BlobsAction {
126126
@Override
127-
public void run(Storage storage, Blob... blobs) {
128-
if (blobs.length == 1) {
129-
if (blobs[0].info().name().isEmpty()) {
127+
public void run(Storage storage, BlobId... blobIds) {
128+
if (blobIds.length == 1) {
129+
if (blobIds[0].name().isEmpty()) {
130130
// get Bucket
131-
Bucket bucket = Bucket.load(storage, blobs[0].info().bucket());
131+
Bucket bucket = Bucket.load(storage, blobIds[0].bucket());
132+
if (bucket == null) {
133+
System.out.println("No such bucket");
134+
return;
135+
}
132136
System.out.println("Bucket info: " + bucket.info());
133137
} else {
134138
// get Blob
135-
System.out.println("Blob info: " + blobs[0].info());
139+
Blob blob = Blob.load(storage, blobIds[0]);
140+
if (blob == null) {
141+
System.out.println("No such object");
142+
return;
143+
}
144+
System.out.println("Blob info: " + blob.info());
136145
}
137146
} else {
138147
// use batch to get multiple blobs.
139-
BatchRequest.Builder batch = BatchRequest.builder();
148+
List<Blob> blobs = Blob.get(storage, blobIds);
140149
for (Blob blob : blobs) {
141-
batch.get(blob.info().bucket(), blob.info().name());
142-
}
143-
BatchResponse response = storage.apply(batch.build());
144-
for (BatchResponse.Result<BlobInfo> result : response.gets()) {
145-
System.out.println(result.get());
150+
if (blob != null) {
151+
System.out.println(blob.info());
152+
}
146153
}
147154
}
148155
}
149156

150157
@Override
151-
Blob[] parse(Storage storage, String... args) {
158+
BlobId[] parse(String... args) {
152159
if (args.length < 2) {
153-
return new Blob[] {new Blob(storage, BlobInfo.builder(args[0], "").build())};
160+
return new BlobId[] {BlobId.of(args[0], "")};
154161
}
155-
return super.parse(storage, args);
162+
return super.parse(args);
156163
}
157164

158165
@Override
@@ -170,24 +177,20 @@ public String params() {
170177
*/
171178
private static class DeleteAction extends BlobsAction {
172179
@Override
173-
public void run(Storage storage, Blob... blobs) {
174-
if (blobs.length == 1) {
175-
boolean wasDeleted = blobs[0].delete();
176-
if (wasDeleted) {
177-
System.out.println("Blob " + blobs[0].info() + " was deleted");
180+
public void run(Storage storage, BlobId... blobIds) {
181+
if (blobIds.length == 1) {
182+
Blob blob = Blob.load(storage, blobIds[0]);
183+
if (blob != null && blob.delete()) {
184+
System.out.println("Blob " + blob.info() + " was deleted");
178185
}
179186
} else {
180187
// use batch operation
181-
BatchRequest.Builder batch = BatchRequest.builder();
182-
for (Blob blob : blobs) {
183-
batch.delete(blob.info().bucket(), blob.info().name());
184-
}
188+
List<Boolean> deleteResults = Blob.delete(storage, blobIds);
185189
int index = 0;
186-
BatchResponse response = storage.apply(batch.build());
187-
for (BatchResponse.Result<Boolean> result : response.deletes()) {
188-
if (result.get()) {
190+
for (Boolean deleted : deleteResults) {
191+
if (deleted) {
189192
// request order is maintained
190-
System.out.println("Blob " + blobs[index].info() + " was deleted");
193+
System.out.println("Blob " + blobIds[index] + " was deleted");
191194
}
192195
index++;
193196
}
@@ -203,7 +206,7 @@ public void run(Storage storage, Blob... blobs) {
203206
private static class ListAction extends StorageAction<String> {
204207

205208
@Override
206-
String parse(Storage storage, String... args) {
209+
String parse(String... args) {
207210
if (args.length == 0) {
208211
return null;
209212
}
@@ -223,6 +226,10 @@ public void run(Storage storage, String bucketName) {
223226
} else {
224227
// list a bucket's blobs
225228
Bucket bucket = Bucket.load(storage, bucketName);
229+
if (bucket == null) {
230+
System.out.println("No such bucket");
231+
return;
232+
}
226233
for (Blob b : bucket.list()) {
227234
System.out.println(b.info());
228235
}
@@ -240,16 +247,17 @@ public String params() {
240247
*
241248
* @see <a href="https://cloud.google.com/storage/docs/json_api/v1/objects/insert">Objects: insert</a>
242249
*/
243-
private static class UploadAction extends StorageAction<Tuple<Path, Blob>> {
250+
private static class UploadAction extends StorageAction<Tuple<Path, BlobInfo>> {
244251
@Override
245-
public void run(Storage storage, Tuple<Path, Blob> tuple) throws Exception {
252+
public void run(Storage storage, Tuple<Path, BlobInfo> tuple) throws Exception {
246253
run(storage, tuple.x(), tuple.y());
247254
}
248255

249-
private void run(Storage storage, Path uploadFrom, Blob blob) throws IOException {
256+
private void run(Storage storage, Path uploadFrom, BlobInfo blobInfo) throws IOException {
250257
if (Files.size(uploadFrom) > 1_000_000) {
251258
// When content is not available or large (1MB or more) it is recommended
252259
// to write it in chunks via the blob's channel writer.
260+
Blob blob = new Blob(storage, blobInfo);
253261
try (BlobWriteChannel writer = blob.writer()) {
254262
byte[] buffer = new byte[1024];
255263
try (InputStream input = Files.newInputStream(uploadFrom)) {
@@ -266,21 +274,20 @@ private void run(Storage storage, Path uploadFrom, Blob blob) throws IOException
266274
} else {
267275
byte[] bytes = Files.readAllBytes(uploadFrom);
268276
// create the blob in one request.
269-
storage.create(blob.info(), bytes);
277+
storage.create(blobInfo, bytes);
270278
}
271279
System.out.println("Blob was created");
272280
}
273281

274282
@Override
275-
Tuple<Path, Blob> parse(Storage storage, String... args) throws IOException {
283+
Tuple<Path, BlobInfo> parse(String... args) throws IOException {
276284
if (args.length < 2 || args.length > 3) {
277285
throw new IllegalArgumentException();
278286
}
279287
Path path = Paths.get(args[0]);
280288
String contentType = Files.probeContentType(path);
281289
String blob = args.length < 3 ? path.getFileName().toString() : args[2];
282-
BlobInfo info = BlobInfo.builder(args[1], blob).contentType(contentType).build();
283-
return Tuple.of(path, new Blob(storage, info));
290+
return Tuple.of(path, BlobInfo.builder(args[1], blob).contentType(contentType).build());
284291
}
285292

286293
@Override
@@ -296,15 +303,16 @@ public String params() {
296303
*
297304
* @see <a href="https://cloud.google.com/storage/docs/json_api/v1/objects/get">Objects: get</a>
298305
*/
299-
private static class DownloadAction extends StorageAction<Tuple<Blob, Path>> {
306+
private static class DownloadAction extends StorageAction<Tuple<BlobId, Path>> {
300307

301308
@Override
302-
public void run(Storage storage, Tuple<Blob, Path> tuple) throws IOException {
309+
public void run(Storage storage, Tuple<BlobId, Path> tuple) throws IOException {
303310
run(storage, tuple.x(), tuple.y());
304311
}
305312

306-
private void run(Storage storage, Blob blob, Path downloadTo) throws IOException {
307-
if (!blob.exists()) {
313+
private void run(Storage storage, BlobId blobId, Path downloadTo) throws IOException {
314+
Blob blob = Blob.load(storage, blobId);
315+
if (blob == null) {
308316
System.out.println("No such object");
309317
return;
310318
}
@@ -336,7 +344,7 @@ private void run(Storage storage, Blob blob, Path downloadTo) throws IOException
336344
}
337345

338346
@Override
339-
Tuple<Blob, Path> parse(Storage storage, String... args) {
347+
Tuple<BlobId, Path> parse(String... args) {
340348
if (args.length < 2 || args.length > 3) {
341349
throw new IllegalArgumentException();
342350
}
@@ -349,7 +357,7 @@ Tuple<Blob, Path> parse(Storage storage, String... args) {
349357
} else {
350358
path = null;
351359
}
352-
return Tuple.of(Blob.load(storage, args[0], args[1]), path);
360+
return Tuple.of(BlobId.of(args[0], args[1]), path);
353361
}
354362

355363
@Override
@@ -371,7 +379,7 @@ public void run(Storage storage, CopyRequest request) {
371379
}
372380

373381
@Override
374-
CopyRequest parse(Storage storage, String... args) {
382+
CopyRequest parse(String... args) {
375383
if (args.length != 4) {
376384
throw new IllegalArgumentException();
377385
}
@@ -397,7 +405,7 @@ public void run(Storage storage, ComposeRequest request) {
397405
}
398406

399407
@Override
400-
ComposeRequest parse(Storage storage, String... args) {
408+
ComposeRequest parse(String... args) {
401409
if (args.length < 3) {
402410
throw new IllegalArgumentException();
403411
}
@@ -421,16 +429,17 @@ public String params() {
421429
* @see <a href="https://cloud.google.com/storage/docs/json_api/v1/objects/update">Objects: update</a>
422430
*/
423431
private static class UpdateMetadataAction extends
424-
StorageAction<Tuple<Blob, Map<String, String>>> {
432+
StorageAction<Tuple<BlobId, Map<String, String>>> {
425433

426434
@Override
427-
public void run(Storage storage, Tuple<Blob, Map<String, String>> tuple)
435+
public void run(Storage storage, Tuple<BlobId, Map<String, String>> tuple)
428436
throws IOException {
429437
run(storage, tuple.x(), tuple.y());
430438
}
431439

432-
private void run(Storage storage, Blob blob, Map<String, String> metadata) {
433-
if (!blob.exists()) {
440+
private void run(Storage storage, BlobId blobId, Map<String, String> metadata) {
441+
Blob blob = Blob.load(storage, blobId);
442+
if (blob == null) {
434443
System.out.println("No such object");
435444
return;
436445
}
@@ -439,11 +448,11 @@ private void run(Storage storage, Blob blob, Map<String, String> metadata) {
439448
}
440449

441450
@Override
442-
Tuple<Blob, Map<String, String>> parse(Storage storage, String... args) {
451+
Tuple<BlobId, Map<String, String>> parse(String... args) {
443452
if (args.length < 2) {
444453
throw new IllegalArgumentException();
445454
}
446-
Blob blob = Blob.load(storage, args[0], args[1]);
455+
BlobId blobId = BlobId.of(args[0], args[1]);
447456
Map<String, String> metadata = new HashMap<>();
448457
for (int i = 2; i < args.length; i++) {
449458
int idx = args[i].indexOf('=');
@@ -453,7 +462,7 @@ Tuple<Blob, Map<String, String>> parse(Storage storage, String... args) {
453462
metadata.put(args[i].substring(0, idx), args[i].substring(idx + 1));
454463
}
455464
}
456-
return Tuple.of(blob, metadata);
465+
return Tuple.of(blobId, metadata);
457466
}
458467

459468
@Override
@@ -469,25 +478,26 @@ public String params() {
469478
* @see <a href="https://cloud.google.com/storage/docs/access-control#Signed-URLs">Signed URLs</a>
470479
*/
471480
private static class SignUrlAction extends
472-
StorageAction<Tuple<ServiceAccountAuthCredentials, Blob>> {
481+
StorageAction<Tuple<ServiceAccountAuthCredentials, BlobInfo>> {
473482

474483
private static final char[] PASSWORD = "notasecret".toCharArray();
475484

476485
@Override
477-
public void run(Storage storage, Tuple<ServiceAccountAuthCredentials, Blob> tuple)
486+
public void run(Storage storage, Tuple<ServiceAccountAuthCredentials, BlobInfo> tuple)
478487
throws Exception {
479488
run(storage, tuple.x(), tuple.y());
480489
}
481490

482-
private void run(Storage storage, ServiceAccountAuthCredentials cred, Blob blob)
491+
private void run(Storage storage, ServiceAccountAuthCredentials cred, BlobInfo blobInfo)
483492
throws IOException {
493+
Blob blob = new Blob(storage, blobInfo);
484494
System.out.println("Signed URL: " +
485495
blob.signUrl(1, TimeUnit.DAYS, SignUrlOption.serviceAccount(cred)));
486496
}
487497

488498
@Override
489-
Tuple<ServiceAccountAuthCredentials, Blob> parse(Storage storage, String... args)
490-
throws IOException, KeyStoreException, CertificateException, NoSuchAlgorithmException,
499+
Tuple<ServiceAccountAuthCredentials, BlobInfo> parse(String... args) throws IOException,
500+
KeyStoreException, CertificateException, NoSuchAlgorithmException,
491501
UnrecoverableKeyException {
492502
if (args.length != 4) {
493503
throw new IllegalArgumentException();
@@ -496,7 +506,7 @@ Tuple<ServiceAccountAuthCredentials, Blob> parse(Storage storage, String... args
496506
keystore.load(Files.newInputStream(Paths.get(args[0])), PASSWORD);
497507
PrivateKey privateKey = (PrivateKey) keystore.getKey("privatekey", PASSWORD);
498508
ServiceAccountAuthCredentials cred = AuthCredentials.createFor(args[1], privateKey);
499-
return Tuple.of(cred, Blob.load(storage, args[2], args[3]));
509+
return Tuple.of(cred, BlobInfo.builder(BlobId.of(args[2], args[3])).build());
500510
}
501511

502512
@Override
@@ -557,7 +567,7 @@ public static void main(String... args) throws Exception {
557567
Storage storage = StorageFactory.instance().get(optionsBuilder.build());
558568
Object request;
559569
try {
560-
request = action.parse(storage, args);
570+
request = action.parse(args);
561571
} catch (IllegalArgumentException ex) {
562572
System.out.println("Invalid input for action '" + args[1] + "'");
563573
System.out.println("Expected: " + action.params());

0 commit comments

Comments
 (0)