16
16
17
17
package com .google .gcloud .examples ;
18
18
19
+ import com .google .gcloud .AuthCredentials ;
20
+ import com .google .gcloud .AuthCredentials .ServiceAccountAuthCredentials ;
19
21
import com .google .gcloud .RetryParams ;
20
22
import com .google .gcloud .spi .StorageRpc .Tuple ;
21
23
import com .google .gcloud .storage .BatchRequest ;
27
29
import com .google .gcloud .storage .StorageService ;
28
30
import com .google .gcloud .storage .StorageService .ComposeRequest ;
29
31
import com .google .gcloud .storage .StorageService .CopyRequest ;
32
+ import com .google .gcloud .storage .StorageService .SignUrlOption ;
30
33
import com .google .gcloud .storage .StorageServiceFactory ;
31
34
import com .google .gcloud .storage .StorageServiceOptions ;
32
35
40
43
import java .nio .file .Files ;
41
44
import java .nio .file .Path ;
42
45
import java .nio .file .Paths ;
46
+ import java .security .KeyStore ;
47
+ import java .security .KeyStoreException ;
48
+ import java .security .NoSuchAlgorithmException ;
49
+ import java .security .PrivateKey ;
50
+ import java .security .UnrecoverableKeyException ;
51
+ import java .security .cert .CertificateException ;
43
52
import java .util .Arrays ;
53
+ import java .util .Calendar ;
44
54
import java .util .HashMap ;
45
55
import java .util .Map ;
46
56
58
68
* -Dexec.args="[<project_id>] list [<bucket>]| info [<bucket> [<file>]]|
59
69
* download <bucket> <path> [local_file]| upload <local_file> <bucket> [<path>]|
60
70
* delete <bucket> <path>+| cp <from_bucket> <from_path> <to_bucket> <to_path>|
61
- * compose <bucket> <from_path>+ <to_path>| update_metadata <bucket> <file> [key=value]*"}
71
+ * compose <bucket> <from_path>+ <to_path>| update_metadata <bucket> <file> [key=value]*|
72
+ * sign_url <service_account_private_key_file> <service_account_email> <bucket> <path>"}
62
73
* </li>
63
74
* </ol>
64
75
*
@@ -75,7 +86,7 @@ private static abstract class StorageAction<T> {
75
86
76
87
abstract void run (StorageService storage , T request ) throws Exception ;
77
88
78
- abstract T parse (String ... args ) throws IllegalArgumentException , IOException ;
89
+ abstract T parse (String ... args ) throws Exception ;
79
90
80
91
protected String params () {
81
92
return "" ;
@@ -424,7 +435,7 @@ public String params() {
424
435
*
425
436
* @see <a href="https://cloud.google.com/storage/docs/json_api/v1/objects/update">Objects: update</a>
426
437
*/
427
- private static class UpdateMetadata extends StorageAction <Tuple <Blob , Map <String , String >>> {
438
+ private static class UpdateMetadataAction extends StorageAction <Tuple <Blob , Map <String , String >>> {
428
439
429
440
@ Override
430
441
public void run (StorageService storage , Tuple <Blob , Map <String , String >> tuple )
@@ -467,6 +478,52 @@ public String params() {
467
478
}
468
479
}
469
480
481
+ /**
482
+ * This class demonstrates how to sign a url.
483
+ * URL will be valid for 1 day.
484
+ *
485
+ * @see <a href="https://cloud.google.com/storage/docs/access-control#Signed-URLs">Signed URLs</a>
486
+ */
487
+ private static class SignUrlAction extends
488
+ StorageAction <Tuple <ServiceAccountAuthCredentials , Blob >> {
489
+
490
+ private static final char [] PASSWORD = "notasecret" .toCharArray ();
491
+
492
+ @ Override
493
+ public void run (StorageService storage , Tuple <ServiceAccountAuthCredentials , Blob > tuple )
494
+ throws Exception {
495
+ run (storage , tuple .x (), tuple .y ());
496
+ }
497
+
498
+ private void run (StorageService storage , ServiceAccountAuthCredentials cred , Blob blob )
499
+ throws IOException {
500
+ Calendar cal = Calendar .getInstance ();
501
+ cal .add (Calendar .DATE , 1 );
502
+ long expiration = cal .getTimeInMillis () / 1000 ;
503
+ System .out .println ("Signed URL: " +
504
+ storage .signUrl (blob , expiration , SignUrlOption .serviceAccount (cred )));
505
+ }
506
+
507
+ @ Override
508
+ Tuple <ServiceAccountAuthCredentials , Blob > parse (String ... args )
509
+ throws IOException , KeyStoreException , CertificateException , NoSuchAlgorithmException ,
510
+ UnrecoverableKeyException {
511
+ if (args .length != 4 ) {
512
+ throw new IllegalArgumentException ();
513
+ }
514
+ KeyStore keystore = KeyStore .getInstance ("PKCS12" );
515
+ keystore .load (Files .newInputStream (Paths .get (args [0 ])), PASSWORD );
516
+ PrivateKey privateKey = (PrivateKey ) keystore .getKey ("privatekey" , PASSWORD );
517
+ ServiceAccountAuthCredentials cred = AuthCredentials .createFor (args [1 ], privateKey );
518
+ return Tuple .of (cred , Blob .of (args [2 ], args [3 ]));
519
+ }
520
+
521
+ @ Override
522
+ public String params () {
523
+ return "<service_account_private_key_file> <service_account_email> <bucket> <path>" ;
524
+ }
525
+ }
526
+
470
527
static {
471
528
ACTIONS .put ("info" , new InfoAction ());
472
529
ACTIONS .put ("delete" , new DeleteAction ());
@@ -475,7 +532,8 @@ public String params() {
475
532
ACTIONS .put ("download" , new DownloadAction ());
476
533
ACTIONS .put ("cp" , new CopyAction ());
477
534
ACTIONS .put ("compose" , new ComposeAction ());
478
- ACTIONS .put ("update_metadata" , new UpdateMetadata ());
535
+ ACTIONS .put ("update_metadata" , new UpdateMetadataAction ());
536
+ ACTIONS .put ("sign_url" , new SignUrlAction ());
479
537
}
480
538
481
539
public static void printUsage () {
0 commit comments