forked from googleapis/google-cloud-java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
StorageExample.java
821 lines (750 loc) · 27.5 KB
/
StorageExample.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
/*
* Copyright 2015 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.cloud.examples.storage;
import com.google.cloud.AuthCredentials;
import com.google.cloud.AuthCredentials.ServiceAccountAuthCredentials;
import com.google.cloud.ReadChannel;
import com.google.cloud.WriteChannel;
import com.google.cloud.storage.Acl;
import com.google.cloud.storage.Blob;
import com.google.cloud.storage.BlobId;
import com.google.cloud.storage.BlobInfo;
import com.google.cloud.storage.Bucket;
import com.google.cloud.storage.CopyWriter;
import com.google.cloud.storage.Storage;
import com.google.cloud.storage.Storage.ComposeRequest;
import com.google.cloud.storage.Storage.CopyRequest;
import com.google.cloud.storage.Storage.SignUrlOption;
import com.google.cloud.storage.StorageOptions;
import com.google.cloud.storage.spi.StorageRpc;
import com.google.cloud.storage.spi.StorageRpc.Tuple;
import com.google.common.collect.ImmutableMap;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.nio.ByteBuffer;
import java.nio.channels.Channels;
import java.nio.channels.WritableByteChannel;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.UnrecoverableKeyException;
import java.security.cert.CertificateException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
/**
* An example of using Google Cloud Storage.
*
* <p>This example demonstrates a simple/typical storage usage.
*
* <p>See the
* <a href="https://github.com/GoogleCloudPlatform/google-cloud-java/blob/master/google-cloud-examples/README.md">
* README</a> for compilation instructions. Run this code with
* <pre>{@code target/appassembler/bin/StorageExample [<project_id>]
* list [<bucket>] |
* info [<bucket> [<file>]] |
* download <bucket> <path> [local_file] |
* upload <local_file> <bucket> [<path>] |
* delete <bucket> <path>+ |
* cp <from_bucket> <from_path> <to_bucket> <to_path> |
* compose <bucket> <from_path>+ <to_path> |
* update_metadata <bucket> <file> [key=value]* |
* sign_url <service_account_private_key_file> <service_account_email> <bucket> <path> |
* add-acl domain <bucket> <path>? <domain> OWNER|READER|WRITER |
* add-acl project <bucket> <path>? <projectId>:(OWNERS|EDITORS|VIEWERS) OWNER|READER|WRITER |
* add-acl user <bucket> <path>? <userEmail>|allUsers|allAuthenticatedUsers OWNER|READER|WRITER |
* add-acl group <bucket> <path>? <group> OWNER|READER|WRITER}</pre>
*
* <p>The first parameter is an optional {@code project_id} (logged-in project will be used if not
* supplied). Second parameter is a Storage operation (list, delete, compose,...) and can be used to
* demonstrate its usage. Any other arguments are specific to the operation. See each action's run
* method for the specific Storage interaction.
*/
public class StorageExample {
private static final Map<String, StorageAction> ACTIONS = new HashMap<>();
private static final Map<String, StorageAction> ACL_ACTIONS = new HashMap<>();
private abstract static class StorageAction<T> {
abstract void run(Storage storage, T arg) throws Exception;
abstract T parse(String... args) throws Exception;
protected String params() {
return "";
}
}
private abstract static class BlobsAction extends StorageAction<BlobId[]> {
@Override
BlobId[] parse(String... args) {
if (args.length < 2) {
throw new IllegalArgumentException();
}
BlobId[] blobs = new BlobId[args.length - 1];
for (int i = 1; i < args.length; i++) {
blobs[i - 1] = BlobId.of(args[0], args[i]);
}
return blobs;
}
@Override
public String params() {
return "<bucket> <path>+";
}
}
private static class ParentAction extends StorageAction<StorageRpc.Tuple<StorageAction, Object>> {
private final Map<String, StorageAction> subActions;
ParentAction(Map<String, StorageAction> subActions) {
this.subActions = ImmutableMap.copyOf(subActions);
}
@Override
@SuppressWarnings("unchecked")
void run(Storage storage, StorageRpc.Tuple<StorageAction, Object> subaction) throws Exception {
subaction.x().run(storage, subaction.y());
}
@Override
StorageRpc.Tuple<StorageAction, Object> parse(String... args) throws Exception {
if (args.length >= 1) {
StorageAction action = subActions.get(args[0]);
if (action != null) {
Object actionArguments = action.parse(Arrays.copyOfRange(args, 1, args.length));
return StorageRpc.Tuple.of(action, actionArguments);
} else {
throw new IllegalArgumentException("Unrecognized entity '" + args[0] + "'.");
}
}
throw new IllegalArgumentException("Missing required entity.");
}
@Override
public String params() {
StringBuilder builder = new StringBuilder();
for (Map.Entry<String, StorageAction> entry : subActions.entrySet()) {
builder.append('\n').append(entry.getKey());
String param = entry.getValue().params();
if (param != null && !param.isEmpty()) {
builder.append(' ').append(param);
}
}
return builder.toString();
}
}
/**
* This class demonstrates how to retrieve Bucket or Blob metadata.
* If more than one blob is supplied a Batch operation would be used to get all blobs metadata
* in a single RPC.
*
* @see <a href="https://cloud.google.com/storage/docs/json_api/v1/objects/get">Objects: get</a>
*/
private static class InfoAction extends BlobsAction {
/**
* Gets information for the provided blobs, using the {@code storage} service. If
* {@code blobIds} contains only one blob identity and {@code blobIds[0].name()} is empty, this
* method gets information for the bucket identified by {@code blobIds[0].bucket()}.
*/
@Override
public void run(Storage storage, BlobId... blobIds) {
if (blobIds.length == 1) {
if (blobIds[0].name().isEmpty()) {
// get Bucket
Bucket bucket = storage.get(blobIds[0].bucket());
if (bucket == null) {
System.out.println("No such bucket");
return;
}
System.out.printf("Bucket info: %s%n", bucket);
} else {
// get Blob
Blob blob = storage.get(blobIds[0]);
if (blob == null) {
System.out.println("No such object");
return;
}
System.out.printf("Blob info: %s%n", blob);
}
} else {
// use batch to get multiple blobs.
List<Blob> blobs = storage.get(blobIds);
for (Blob blob : blobs) {
if (blob != null) {
System.out.println(blob);
}
}
}
}
@Override
BlobId[] parse(String... args) {
if (args.length < 2) {
return new BlobId[] {BlobId.of(args[0], "")};
}
return super.parse(args);
}
@Override
public String params() {
return "<bucket> [<path>+]";
}
}
/**
* This class demonstrates how to delete a blob.
* If more than one blob is supplied a Batch operation would be used to delete all requested
* blobs in a single RPC.
*
* @see <a href="https://cloud.google.com/storage/docs/json_api/v1/objects/delete">Objects: delete</a>
*/
private static class DeleteAction extends BlobsAction {
@Override
public void run(Storage storage, BlobId... blobIds) {
// use batch operation
List<Boolean> deleteResults = storage.delete(blobIds);
int index = 0;
for (Boolean deleted : deleteResults) {
if (deleted) {
// request order is maintained
System.out.printf("Blob %s was deleted%n", blobIds[index]);
}
index++;
}
}
}
/**
* This class demonstrates how to list buckets or a bucket's blobs.
*
* @see <a href="https://cloud.google.com/storage/docs/json_api/v1/objects/list">Objects: list</a>
*/
private static class ListAction extends StorageAction<String> {
@Override
String parse(String... args) {
if (args.length == 0) {
return null;
}
if (args.length == 1) {
return args[0];
}
throw new IllegalArgumentException();
}
@Override
public void run(Storage storage, String bucketName) {
if (bucketName == null) {
// list buckets
Iterator<Bucket> bucketIterator = storage.list().iterateAll();
while (bucketIterator.hasNext()) {
System.out.println(bucketIterator.next());
}
} else {
// list a bucket's blobs
Bucket bucket = storage.get(bucketName);
if (bucket == null) {
System.out.println("No such bucket");
return;
}
Iterator<Blob> blobIterator = bucket.list().iterateAll();
while (blobIterator.hasNext()) {
System.out.println(blobIterator.next());
}
}
}
@Override
public String params() {
return "[<bucket>]";
}
}
/**
* This class demonstrates how to create a new Blob or to update its content.
*
* @see <a href="https://cloud.google.com/storage/docs/json_api/v1/objects/insert">Objects: insert</a>
*/
private static class UploadAction extends StorageAction<Tuple<Path, BlobInfo>> {
@Override
public void run(Storage storage, Tuple<Path, BlobInfo> tuple) throws Exception {
run(storage, tuple.x(), tuple.y());
}
private void run(Storage storage, Path uploadFrom, BlobInfo blobInfo) throws IOException {
if (Files.size(uploadFrom) > 1_000_000) {
// When content is not available or large (1MB or more) it is recommended
// to write it in chunks via the blob's channel writer.
try (WriteChannel writer = storage.writer(blobInfo)) {
byte[] buffer = new byte[1024];
try (InputStream input = Files.newInputStream(uploadFrom)) {
int limit;
while ((limit = input.read(buffer)) >= 0) {
try {
writer.write(ByteBuffer.wrap(buffer, 0, limit));
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
}
} else {
byte[] bytes = Files.readAllBytes(uploadFrom);
// create the blob in one request.
storage.create(blobInfo, bytes);
}
System.out.println("Blob was created");
}
@Override
Tuple<Path, BlobInfo> parse(String... args) throws IOException {
if (args.length < 2 || args.length > 3) {
throw new IllegalArgumentException();
}
Path path = Paths.get(args[0]);
String contentType = Files.probeContentType(path);
String blob = args.length < 3 ? path.getFileName().toString() : args[2];
return Tuple.of(path, BlobInfo.builder(args[1], blob).contentType(contentType).build());
}
@Override
public String params() {
return "<local_file> <bucket> [<path>]";
}
}
/**
* This class demonstrates how read a blob's content.
* The example will dump the content to a local file if one was given or write
* it to stdout otherwise.
*
* @see <a href="https://cloud.google.com/storage/docs/json_api/v1/objects/get">Objects: get</a>
*/
private static class DownloadAction extends StorageAction<Tuple<BlobId, Path>> {
@Override
public void run(Storage storage, Tuple<BlobId, Path> tuple) throws IOException {
run(storage, tuple.x(), tuple.y());
}
private void run(Storage storage, BlobId blobId, Path downloadTo) throws IOException {
Blob blob = storage.get(blobId);
if (blob == null) {
System.out.println("No such object");
return;
}
PrintStream writeTo = System.out;
if (downloadTo != null) {
writeTo = new PrintStream(new FileOutputStream(downloadTo.toFile()));
}
if (blob.size() < 1_000_000) {
// Blob is small read all its content in one request
byte[] content = blob.content();
writeTo.write(content);
} else {
// When Blob size is big or unknown use the blob's channel reader.
try (ReadChannel reader = blob.reader()) {
WritableByteChannel channel = Channels.newChannel(writeTo);
ByteBuffer bytes = ByteBuffer.allocate(64 * 1024);
while (reader.read(bytes) > 0) {
bytes.flip();
channel.write(bytes);
bytes.clear();
}
}
}
if (downloadTo == null) {
writeTo.println();
} else {
writeTo.close();
}
}
@Override
Tuple<BlobId, Path> parse(String... args) {
if (args.length < 2 || args.length > 3) {
throw new IllegalArgumentException();
}
Path path;
if (args.length > 2) {
path = Paths.get(args[2]);
if (Files.isDirectory(path)) {
path = path.resolve(Paths.get(args[1]).getFileName());
}
} else {
path = null;
}
return Tuple.of(BlobId.of(args[0], args[1]), path);
}
@Override
public String params() {
return "<bucket> <path> [local_file]";
}
}
/**
* This class demonstrates how to use the copy command.
*
* @see <a href="https://cloud.google.com/storage/docs/json_api/v1/objects/copy">Objects: copy</a>
*/
private static class CopyAction extends StorageAction<CopyRequest> {
@Override
public void run(Storage storage, CopyRequest request) {
CopyWriter copyWriter = storage.copy(request);
System.out.printf("Copied %s%n", copyWriter.result());
}
@Override
CopyRequest parse(String... args) {
if (args.length != 4) {
throw new IllegalArgumentException();
}
return CopyRequest.of(args[0], args[1], BlobId.of(args[2], args[3]));
}
@Override
public String params() {
return "<from_bucket> <from_path> <to_bucket> <to_path>";
}
}
/**
* This class demonstrates how to use the compose command.
*
* @see <a href="https://cloud.google.com/storage/docs/json_api/v1/objects/compose">Objects: compose</a>
*/
private static class ComposeAction extends StorageAction<ComposeRequest> {
@Override
public void run(Storage storage, ComposeRequest request) {
BlobInfo composedBlobInfo = storage.compose(request);
System.out.printf("Composed %s%n", composedBlobInfo);
}
@Override
ComposeRequest parse(String... args) {
if (args.length < 3) {
throw new IllegalArgumentException();
}
ComposeRequest.Builder request = ComposeRequest.builder();
request.target(BlobInfo.builder(args[0], args[args.length - 1]).build());
for (int i = 1; i < args.length - 1; i++) {
request.addSource(args[i]);
}
return request.build();
}
@Override
public String params() {
return "<bucket> <from_path>+ <to_path>";
}
}
/**
* This class demonstrates how to update a blob's metadata.
*
* @see <a href="https://cloud.google.com/storage/docs/json_api/v1/objects/update">Objects: update</a>
*/
private static class UpdateMetadataAction extends
StorageAction<Tuple<BlobId, Map<String, String>>> {
@Override
public void run(Storage storage, Tuple<BlobId, Map<String, String>> tuple)
throws IOException {
run(storage, tuple.x(), tuple.y());
}
private void run(Storage storage, BlobId blobId, Map<String, String> metadata) {
Blob blob = storage.get(blobId);
if (blob == null) {
System.out.println("No such object");
return;
}
Blob updateBlob = blob.toBuilder().metadata(metadata).build().update();
System.out.printf("Updated %s%n", updateBlob);
}
@Override
Tuple<BlobId, Map<String, String>> parse(String... args) {
if (args.length < 2) {
throw new IllegalArgumentException();
}
BlobId blobId = BlobId.of(args[0], args[1]);
Map<String, String> metadata = new HashMap<>();
for (int i = 2; i < args.length; i++) {
int idx = args[i].indexOf('=');
if (idx < 0) {
metadata.put(args[i], "");
} else {
metadata.put(args[i].substring(0, idx), args[i].substring(idx + 1));
}
}
return Tuple.of(blobId, metadata);
}
@Override
public String params() {
return "<bucket> <path> [local_file]";
}
}
/**
* This class demonstrates how to sign a url.
* URL will be valid for 1 day.
*
* @see <a href="https://cloud.google.com/storage/docs/access-control#Signed-URLs">Signed URLs</a>
*/
private static class SignUrlAction extends
StorageAction<Tuple<ServiceAccountAuthCredentials, BlobInfo>> {
private static final char[] PASSWORD = "notasecret".toCharArray();
@Override
public void run(Storage storage, Tuple<ServiceAccountAuthCredentials, BlobInfo> tuple)
throws Exception {
run(storage, tuple.x(), tuple.y());
}
private void run(Storage storage, ServiceAccountAuthCredentials cred, BlobInfo blobInfo) {
Blob blob = storage.get(blobInfo.blobId());
System.out.printf("Signed URL: %s%n",
blob.signUrl(1, TimeUnit.DAYS, SignUrlOption.signWith(cred)));
}
@Override
Tuple<ServiceAccountAuthCredentials, BlobInfo> parse(String... args) throws IOException,
KeyStoreException, CertificateException, NoSuchAlgorithmException,
UnrecoverableKeyException {
if (args.length != 4) {
throw new IllegalArgumentException();
}
KeyStore keystore = KeyStore.getInstance("PKCS12");
keystore.load(Files.newInputStream(Paths.get(args[0])), PASSWORD);
PrivateKey privateKey = (PrivateKey) keystore.getKey("privatekey", PASSWORD);
ServiceAccountAuthCredentials cred = AuthCredentials.createFor(args[1], privateKey);
return Tuple.of(cred, BlobInfo.builder(BlobId.of(args[2], args[3])).build());
}
@Override
public String params() {
return "<service_account_private_key_file> <service_account_email> <bucket> <path>";
}
}
private abstract static class AclAction extends StorageAction<Tuple<BlobId, Acl>> {
/**
* Sets the ACL according to the provided {@code params}, using the {@code storage} service. If
* {@code params.x()} returns a complete blob identity, the {@code params.y()} ACL is added to
* the blob. If {@code params.x().name()} is empty, the {@code params.y()} ACL is added to the
* bucket identified by {@code params.x().bucket()}.
*/
@Override
public void run(Storage storage, Tuple<BlobId, Acl> params) {
BlobId blobId = params.x();
Acl acl = params.y();
if (blobId.name().isEmpty()) {
Bucket bucket = storage.get(blobId.bucket());
if (bucket == null) {
System.out.printf("Bucket %s does not exist%n", blobId.bucket());
return;
}
acl = bucket.createAcl(acl);
System.out.printf("Added ACL %s to bucket %s%n", acl, blobId.bucket());
} else {
Blob blob = storage.get(blobId);
if (blob == null) {
System.out.printf("Blob %s does not exist%n", blobId);
return;
}
acl = blob.createAcl(acl);
System.out.printf("Added ACL %s to blob %s%n", acl, blobId);
}
}
}
/**
* This class demonstrates how to add an ACL to a blob or a bucket for a group of users
* (identified by the group's email).
*
* @see <a href="https://cloud.google.com/storage/docs/access-control/lists#permissions">Access
* Control Lists (ACLs)</a>
*/
private static class AddGroupAclAction extends AclAction {
@Override
Tuple<BlobId, Acl> parse(String... args) {
if (args.length >= 3) {
BlobId blob;
int nextArg;
if (args.length == 3) {
blob = BlobId.of(args[0], "");
nextArg = 1;
} else if (args.length == 4) {
blob = BlobId.of(args[0], args[1]);
nextArg = 2;
} else {
throw new IllegalArgumentException("Too many arguments.");
}
String group = args[nextArg++];
Acl.Role role = Acl.Role.valueOf(args[nextArg]);
return Tuple.of(blob, Acl.of(new Acl.Group(group), role));
}
throw new IllegalArgumentException("Missing required bucket, groupEmail or role arguments.");
}
@Override
public String params() {
return "<bucket> <path>? <group> OWNER|READER|WRITER";
}
}
/**
* This class demonstrates how to add an ACL to a blob or a bucket for a domain.
*
* @see <a href="https://cloud.google.com/storage/docs/access-control/lists#permissions">Access
* Control Lists (ACLs)</a>
*/
private static class AddDomainAclAction extends AclAction {
@Override
Tuple<BlobId, Acl> parse(String... args) {
if (args.length >= 3) {
BlobId blob;
int nextArg;
if (args.length == 3) {
blob = BlobId.of(args[0], "");
nextArg = 1;
} else if (args.length == 4) {
blob = BlobId.of(args[0], args[1]);
nextArg = 2;
} else {
throw new IllegalArgumentException("Too many arguments.");
}
String domain = args[nextArg++];
Acl.Role role = Acl.Role.valueOf(args[nextArg]);
return Tuple.of(blob, Acl.of(new Acl.Domain(domain), role));
}
throw new IllegalArgumentException("Missing required bucket, domain or role arguments.");
}
@Override
public String params() {
return "<bucket> <path>? <domain> OWNER|READER|WRITER";
}
}
/**
* This class demonstrates how to add an ACL to a blob or a bucket for either a user (if an email
* is provided), all users (if {@code allUsers} is provided), or all authenticated users (if
* {@code allAuthenticatedUsers} is provided).
*
* @see <a href="https://cloud.google.com/storage/docs/access-control/lists#permissions">Access
* Control Lists (ACLs)</a>
*/
private static class AddUserAclAction extends AclAction {
@Override
Tuple<BlobId, Acl> parse(String... args) {
if (args.length >= 3) {
BlobId blob;
int nextArg;
if (args.length == 3) {
blob = BlobId.of(args[0], "");
nextArg = 1;
} else if (args.length == 4) {
blob = BlobId.of(args[0], args[1]);
nextArg = 2;
} else {
throw new IllegalArgumentException("Too many arguments.");
}
String user = args[nextArg++];
Acl.Role role = Acl.Role.valueOf(args[nextArg]);
return Tuple.of(blob, Acl.of(new Acl.User(user), role));
}
throw new IllegalArgumentException("Missing required bucket, userEmail or role arguments.");
}
@Override
public String params() {
return "<bucket> <path>? <userEmail>|allUsers|allAuthenticatedUsers OWNER|READER|WRITER";
}
}
/**
* This class demonstrates how to add an ACL to a blob or a bucket for all users that have a
* specific role in a provided project.
*
* @see <a href="https://cloud.google.com/storage/docs/access-control/lists#permissions">Access
* Control Lists (ACLs)</a>
*/
private static class AddProjectAclAction extends AclAction {
@Override
Tuple<BlobId, Acl> parse(String... args) {
if (args.length >= 3) {
BlobId blob;
int nextArg;
if (args.length == 3) {
blob = BlobId.of(args[0], "");
nextArg = 1;
} else if (args.length == 4) {
blob = BlobId.of(args[0], args[1]);
nextArg = 2;
} else {
throw new IllegalArgumentException("Too many arguments.");
}
String[] projectAndRole = args[nextArg++].split(":");
if (projectAndRole.length != 2) {
throw new IllegalArgumentException(
"Project entity must be specified as <projectId>:(OWNERS|READERS|WRITERS)");
} else {
Acl.Project.ProjectRole projectRole = Acl.Project.ProjectRole.valueOf(projectAndRole[1]);
Acl.Role role = Acl.Role.valueOf(args[nextArg]);
return Tuple.of(blob, Acl.of(new Acl.Project(projectRole, projectAndRole[0]), role));
}
}
throw new IllegalArgumentException("Missing required bucket, project or role arguments.");
}
@Override
public String params() {
return "<bucket> <path>? <projectId>:(OWNERS|EDITORS|VIEWERS) OWNER|READER|WRITER";
}
}
static {
ACTIONS.put("info", new InfoAction());
ACTIONS.put("delete", new DeleteAction());
ACTIONS.put("list", new ListAction());
ACTIONS.put("upload", new UploadAction());
ACTIONS.put("download", new DownloadAction());
ACTIONS.put("cp", new CopyAction());
ACTIONS.put("compose", new ComposeAction());
ACTIONS.put("update_metadata", new UpdateMetadataAction());
ACTIONS.put("sign_url", new SignUrlAction());
ACL_ACTIONS.put("group", new AddGroupAclAction());
ACL_ACTIONS.put("domain", new AddDomainAclAction());
ACL_ACTIONS.put("user", new AddUserAclAction());
ACL_ACTIONS.put("project", new AddProjectAclAction());
ACTIONS.put("add-acl", new ParentAction(ACL_ACTIONS));
}
private static void printUsage() {
StringBuilder actionAndParams = new StringBuilder();
for (Map.Entry<String, StorageAction> entry : ACTIONS.entrySet()) {
actionAndParams.append("\n\t").append(entry.getKey());
String param = entry.getValue().params();
if (param != null && !param.isEmpty()) {
// Add extra padding for multi-line action
actionAndParams.append(' ').append(param.replace("\n", "\n\t\t"));
}
}
System.out.printf("Usage: %s [<project_id>] operation [entity] <args>*%s%n",
StorageExample.class.getSimpleName(), actionAndParams);
}
@SuppressWarnings("unchecked")
public static void main(String... args) throws Exception {
if (args.length < 1) {
System.out.println("Missing required project id and action");
printUsage();
return;
}
StorageOptions.Builder optionsBuilder = StorageOptions.builder();
StorageAction action;
String actionName;
if (args.length >= 2 && !ACTIONS.containsKey(args[0])) {
actionName = args[1];
optionsBuilder.projectId(args[0]);
action = ACTIONS.get(args[1]);
args = Arrays.copyOfRange(args, 2, args.length);
} else {
actionName = args[0];
action = ACTIONS.get(args[0]);
args = Arrays.copyOfRange(args, 1, args.length);
}
if (action == null) {
System.out.println("Unrecognized action.");
printUsage();
return;
}
Storage storage = optionsBuilder.build().service();
Object arg;
try {
arg = action.parse(args);
} catch (IllegalArgumentException ex) {
System.out.printf("Invalid input for action '%s'. %s%n", actionName, ex.getMessage());
System.out.printf("Expected: %s%n", action.params());
return;
} catch (Exception ex) {
System.out.println("Failed to parse arguments.");
ex.printStackTrace();
return;
}
action.run(storage, arg);
}
}