diff --git a/gcloud-java-examples/src/main/java/com/google/gcloud/examples/StorageExample.java b/gcloud-java-examples/src/main/java/com/google/gcloud/examples/StorageExample.java index 2ac1dcacce52..99b543107483 100644 --- a/gcloud-java-examples/src/main/java/com/google/gcloud/examples/StorageExample.java +++ b/gcloud-java-examples/src/main/java/com/google/gcloud/examples/StorageExample.java @@ -88,21 +88,21 @@ private static abstract class StorageAction { abstract void run(Storage storage, T request) throws Exception; - abstract T parse(String... args) throws Exception; + abstract T parse(Storage storage, String... args) throws Exception; protected String params() { return ""; } } - private static abstract class BlobAction extends StorageAction { + private static abstract class BlobAction extends StorageAction { @Override - BlobInfo parse(String... args) { + Blob parse(Storage storage, String... args) { if (args.length != 2) { throw new IllegalArgumentException(); } - return BlobInfo.of(args[0], args[1]); + return new Blob(storage, args[0], args[1]); } @Override @@ -111,18 +111,18 @@ public String params() { } } - private static abstract class BlobsAction extends StorageAction { + private static abstract class BlobsAction extends StorageAction { @Override - BlobInfo[] parse(String... args) { + Blob[] parse(Storage storage, String... args) { if (args.length < 2) { throw new IllegalArgumentException(); } - BlobInfo[] blobInfos = new BlobInfo[args.length - 1]; + Blob[] blobs = new Blob[args.length - 1]; for (int i = 1; i < args.length; i++) { - blobInfos[i - 1] = BlobInfo.of(args[0], args[i]); + blobs[i - 1] = new Blob(storage, args[0], args[i]); } - return blobInfos; + return blobs; } @Override @@ -140,22 +140,21 @@ public String params() { */ private static class InfoAction extends BlobsAction { @Override - public void run(Storage storage, BlobInfo... blobInfos) { - if (blobInfos.length == 1) { - if (blobInfos[0].name().isEmpty()) { + public void run(Storage storage, Blob... blobs) { + if (blobs.length == 1) { + if (blobs[0].info().name().isEmpty()) { // get Bucket - Bucket bucket = new Bucket(storage, blobInfos[0].bucket()); + Bucket bucket = new Bucket(storage, blobs[0].info().bucket()); System.out.println("Bucket info: " + bucket.reload().info()); } else { // get Blob - Blob blob = new Blob(storage, blobInfos[0]); - System.out.println("Blob info: " + blob.reload().info()); + System.out.println("Blob info: " + blobs[0].reload().info()); } } else { // use batch to get multiple blobs. BatchRequest.Builder batch = BatchRequest.builder(); - for (BlobInfo blobInfo : blobInfos) { - batch.get(blobInfo.bucket(), blobInfo.name()); + for (Blob blob : blobs) { + batch.get(blob.info().bucket(), blob.info().name()); } BatchResponse response = storage.apply(batch.build()); for (BatchResponse.Result result : response.gets()) { @@ -165,11 +164,11 @@ public void run(Storage storage, BlobInfo... blobInfos) { } @Override - BlobInfo[] parse(String... args) { + Blob[] parse(Storage storage, String... args) { if (args.length < 2) { - return new BlobInfo[] {BlobInfo.of(args[0], "")}; + return new Blob[] {new Blob(storage, args[0], "")}; } - return super.parse(args); + return super.parse(storage, args); } @Override @@ -187,24 +186,24 @@ public String params() { */ private static class DeleteAction extends BlobsAction { @Override - public void run(Storage storage, BlobInfo... blobInfos) { - if (blobInfos.length == 1) { - boolean wasDeleted = new Blob(storage, blobInfos[0]).delete(); + public void run(Storage storage, Blob... blobs) { + if (blobs.length == 1) { + boolean wasDeleted = blobs[0].delete(); if (wasDeleted) { - System.out.println("Blob " + blobInfos[0] + " was deleted"); + System.out.println("Blob " + blobs[0].info() + " was deleted"); } } else { // use batch operation BatchRequest.Builder batch = BatchRequest.builder(); - for (BlobInfo blobInfo : blobInfos) { - batch.delete(blobInfo.bucket(), blobInfo.name()); + for (Blob blob : blobs) { + batch.delete(blob.info().bucket(), blob.info().name()); } int index = 0; BatchResponse response = storage.apply(batch.build()); for (BatchResponse.Result result : response.deletes()) { if (result.get()) { // request order is maintained - System.out.println("Blob " + blobInfos[index] + " was deleted"); + System.out.println("Blob " + blobs[index].info() + " was deleted"); } index++; } @@ -220,7 +219,7 @@ public void run(Storage storage, BlobInfo... blobInfos) { private static class ListAction extends StorageAction { @Override - String parse(String... args) { + String parse(Storage storage, String... args) { if (args.length == 0) { return null; } @@ -231,16 +230,16 @@ String parse(String... args) { } @Override - public void run(Storage storage, String bucket) { - if (bucket == null) { + public void run(Storage storage, String bucketName) { + if (bucketName == null) { // list buckets for (BucketInfo b : storage.list()) { System.out.println(b); } } else { // list a bucket's blobs - Bucket functionalBucket = new Bucket(storage, bucket); - for (Blob b : functionalBucket.list()) { + Bucket bucket = new Bucket(storage, bucketName); + for (Blob b : bucket.list()) { System.out.println(b.info()); } } @@ -257,17 +256,16 @@ public String params() { * * @see Objects: insert */ - private static class UploadAction extends StorageAction> { + private static class UploadAction extends StorageAction> { @Override - public void run(Storage storage, Tuple tuple) throws Exception { + public void run(Storage storage, Tuple tuple) throws Exception { run(storage, tuple.x(), tuple.y()); } - private void run(Storage storage, Path uploadFrom, BlobInfo blobInfo) throws IOException { + private void run(Storage storage, Path uploadFrom, Blob blob) 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. - Blob blob = new Blob(storage, blobInfo); try (BlobWriteChannel writer = blob.writer()) { byte[] buffer = new byte[1024]; try (InputStream input = Files.newInputStream(uploadFrom)) { @@ -284,20 +282,21 @@ private void run(Storage storage, Path uploadFrom, BlobInfo blobInfo) throws IOE } else { byte[] bytes = Files.readAllBytes(uploadFrom); // create the blob in one request. - storage.create(blobInfo, bytes); + storage.create(blob.info(), bytes); } System.out.println("Blob was created"); } @Override - Tuple parse(String... args) throws IOException { + Tuple parse(Storage storage, 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()); + BlobInfo info = BlobInfo.builder(args[1], blob).contentType(contentType).build(); + return Tuple.of(path, new Blob(storage, info)); } @Override @@ -313,17 +312,15 @@ public String params() { * * @see Objects: get */ - private static class DownloadAction extends StorageAction> { + private static class DownloadAction extends StorageAction> { @Override - public void run(Storage storage, Tuple tuple) throws IOException { - run(storage, tuple.x().bucket(), tuple.x().name(), tuple.y()); + public void run(Storage storage, Tuple tuple) throws IOException { + run(storage, tuple.x(), tuple.y()); } - private void run(Storage storage, String bucket, String blobName, Path downloadTo) - throws IOException { - Blob blob = new Blob(storage, bucket, blobName); - BlobInfo blobInfo = storage.get(bucket, blobName); + private void run(Storage storage, Blob blob, Path downloadTo) throws IOException { + blob = blob.reload(); if (!blob.exists()) { System.out.println("No such object"); return; @@ -332,7 +329,7 @@ private void run(Storage storage, String bucket, String blobName, Path downloadT if (downloadTo != null) { writeTo = new PrintStream(new FileOutputStream(downloadTo.toFile())); } - if (blobInfo.size() < 1_000_000) { + if (blob.info().size() < 1_000_000) { // Blob is small read all its content in one request byte[] content = blob.content(); writeTo.write(content); @@ -356,7 +353,7 @@ private void run(Storage storage, String bucket, String blobName, Path downloadT } @Override - Tuple parse(String... args) { + Tuple parse(Storage storage, String... args) { if (args.length < 2 || args.length > 3) { throw new IllegalArgumentException(); } @@ -369,7 +366,7 @@ Tuple parse(String... args) { } else { path = null; } - return Tuple.of(BlobInfo.of(args[0], args[1]), path); + return Tuple.of(new Blob(storage, args[0], args[1]), path); } @Override @@ -391,7 +388,7 @@ public void run(Storage storage, CopyRequest request) { } @Override - CopyRequest parse(String... args) { + CopyRequest parse(Storage storage, String... args) { if (args.length != 4) { throw new IllegalArgumentException(); } @@ -417,7 +414,7 @@ public void run(Storage storage, ComposeRequest request) { } @Override - ComposeRequest parse(String... args) { + ComposeRequest parse(Storage storage, String... args) { if (args.length < 3) { throw new IllegalArgumentException(); } @@ -441,17 +438,16 @@ public String params() { * @see Objects: update */ private static class UpdateMetadataAction extends - StorageAction>> { + StorageAction>> { @Override - public void run(Storage storage, Tuple> tuple) + public void run(Storage storage, Tuple> tuple) throws IOException { - run(storage, tuple.x().bucket(), tuple.x().name(), tuple.y()); + run(storage, tuple.x(), tuple.y()); } - private void run(Storage storage, String bucket, String blobName, - Map metadata) { - Blob blob = new Blob(storage, bucket, blobName).reload(); + private void run(Storage storage, Blob blob, Map metadata) { + blob = blob.reload(); if (!blob.exists()) { System.out.println("No such object"); return; @@ -461,11 +457,11 @@ private void run(Storage storage, String bucket, String blobName, } @Override - Tuple> parse(String... args) { + Tuple> parse(Storage storage, String... args) { if (args.length < 2) { throw new IllegalArgumentException(); } - BlobInfo blobInfo = BlobInfo.of(args[0], args[1]); + Blob blob = new Blob(storage, args[0], args[1]); Map metadata = new HashMap<>(); for (int i = 2; i < args.length; i++) { int idx = args[i].indexOf('='); @@ -475,7 +471,7 @@ Tuple> parse(String... args) { metadata.put(args[i].substring(0, idx), args[i].substring(idx + 1)); } } - return Tuple.of(blobInfo, metadata); + return Tuple.of(blob, metadata); } @Override @@ -491,27 +487,27 @@ public String params() { * @see Signed URLs */ private static class SignUrlAction extends - StorageAction> { + StorageAction> { private static final char[] PASSWORD = "notasecret".toCharArray(); @Override - public void run(Storage storage, Tuple tuple) + public void run(Storage storage, Tuple tuple) throws Exception { run(storage, tuple.x(), tuple.y()); } - private void run(Storage storage, ServiceAccountAuthCredentials cred, BlobInfo blobInfo) + private void run(Storage storage, ServiceAccountAuthCredentials cred, Blob blob) throws IOException { Calendar cal = Calendar.getInstance(); cal.add(Calendar.DATE, 1); long expiration = cal.getTimeInMillis() / 1000; System.out.println("Signed URL: " + - new Blob(storage, blobInfo).signUrl(expiration, SignUrlOption.serviceAccount(cred))); + blob.signUrl(expiration, SignUrlOption.serviceAccount(cred))); } @Override - Tuple parse(String... args) + Tuple parse(Storage storage, String... args) throws IOException, KeyStoreException, CertificateException, NoSuchAlgorithmException, UnrecoverableKeyException { if (args.length != 4) { @@ -521,7 +517,7 @@ Tuple parse(String... args) 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.of(args[2], args[3])); + return Tuple.of(cred, new Blob(storage, args[2], args[3])); } @Override @@ -582,7 +578,7 @@ public static void main(String... args) throws Exception { Storage storage = StorageFactory.instance().get(optionsBuilder.build()); Object request; try { - request = action.parse(args); + request = action.parse(storage, args); } catch (IllegalArgumentException ex) { System.out.println("Invalid input for action '" + args[1] + "'"); System.out.println("Expected: " + action.params());