Skip to content

Commit 3011587

Browse files
committed
Add static load method to Blob and Bucket, add tests, update example
1 parent 7115734 commit 3011587

5 files changed

Lines changed: 51 additions & 49 deletions

File tree

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

Lines changed: 9 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@
5252
import java.security.UnrecoverableKeyException;
5353
import java.security.cert.CertificateException;
5454
import java.util.Arrays;
55-
import java.util.Calendar;
5655
import java.util.HashMap;
5756
import java.util.Map;
5857
import java.util.concurrent.TimeUnit;
@@ -96,22 +95,6 @@ protected String params() {
9695
}
9796
}
9897

99-
private static abstract class BlobAction extends StorageAction<Blob> {
100-
101-
@Override
102-
Blob parse(Storage storage, String... args) {
103-
if (args.length != 2) {
104-
throw new IllegalArgumentException();
105-
}
106-
return new Blob(storage, args[0], args[1]);
107-
}
108-
109-
@Override
110-
public String params() {
111-
return "<bucket> <path>";
112-
}
113-
}
114-
11598
private static abstract class BlobsAction extends StorageAction<Blob[]> {
11699

117100
@Override
@@ -121,7 +104,7 @@ Blob[] parse(Storage storage, String... args) {
121104
}
122105
Blob[] blobs = new Blob[args.length - 1];
123106
for (int i = 1; i < args.length; i++) {
124-
blobs[i - 1] = new Blob(storage, args[0], args[i]);
107+
blobs[i - 1] = Blob.load(storage, args[0], args[i]);
125108
}
126109
return blobs;
127110
}
@@ -145,11 +128,11 @@ public void run(Storage storage, Blob... blobs) {
145128
if (blobs.length == 1) {
146129
if (blobs[0].info().name().isEmpty()) {
147130
// get Bucket
148-
Bucket bucket = new Bucket(storage, blobs[0].info().bucket());
149-
System.out.println("Bucket info: " + bucket.reload().info());
131+
Bucket bucket = Bucket.load(storage, blobs[0].info().bucket());
132+
System.out.println("Bucket info: " + bucket.info());
150133
} else {
151134
// get Blob
152-
System.out.println("Blob info: " + blobs[0].reload().info());
135+
System.out.println("Blob info: " + blobs[0].info());
153136
}
154137
} else {
155138
// use batch to get multiple blobs.
@@ -167,7 +150,7 @@ public void run(Storage storage, Blob... blobs) {
167150
@Override
168151
Blob[] parse(Storage storage, String... args) {
169152
if (args.length < 2) {
170-
return new Blob[] {new Blob(storage, args[0], "")};
153+
return new Blob[] {new Blob(storage, BlobInfo.builder(args[0], "").build())};
171154
}
172155
return super.parse(storage, args);
173156
}
@@ -239,7 +222,7 @@ public void run(Storage storage, String bucketName) {
239222
}
240223
} else {
241224
// list a bucket's blobs
242-
Bucket bucket = new Bucket(storage, bucketName);
225+
Bucket bucket = Bucket.load(storage, bucketName);
243226
for (Blob b : bucket.list()) {
244227
System.out.println(b.info());
245228
}
@@ -321,7 +304,6 @@ public void run(Storage storage, Tuple<Blob, Path> tuple) throws IOException {
321304
}
322305

323306
private void run(Storage storage, Blob blob, Path downloadTo) throws IOException {
324-
blob = blob.reload();
325307
if (!blob.exists()) {
326308
System.out.println("No such object");
327309
return;
@@ -367,7 +349,7 @@ Tuple<Blob, Path> parse(Storage storage, String... args) {
367349
} else {
368350
path = null;
369351
}
370-
return Tuple.of(new Blob(storage, args[0], args[1]), path);
352+
return Tuple.of(Blob.load(storage, args[0], args[1]), path);
371353
}
372354

373355
@Override
@@ -448,7 +430,6 @@ public void run(Storage storage, Tuple<Blob, Map<String, String>> tuple)
448430
}
449431

450432
private void run(Storage storage, Blob blob, Map<String, String> metadata) {
451-
blob = blob.reload();
452433
if (!blob.exists()) {
453434
System.out.println("No such object");
454435
return;
@@ -462,7 +443,7 @@ Tuple<Blob, Map<String, String>> parse(Storage storage, String... args) {
462443
if (args.length < 2) {
463444
throw new IllegalArgumentException();
464445
}
465-
Blob blob = new Blob(storage, args[0], args[1]);
446+
Blob blob = Blob.load(storage, args[0], args[1]);
466447
Map<String, String> metadata = new HashMap<>();
467448
for (int i = 2; i < args.length; i++) {
468449
int idx = args[i].indexOf('=');
@@ -515,7 +496,7 @@ Tuple<ServiceAccountAuthCredentials, Blob> parse(Storage storage, String... args
515496
keystore.load(Files.newInputStream(Paths.get(args[0])), PASSWORD);
516497
PrivateKey privateKey = (PrivateKey) keystore.getKey("privatekey", PASSWORD);
517498
ServiceAccountAuthCredentials cred = AuthCredentials.createFor(args[1], privateKey);
518-
return Tuple.of(cred, new Blob(storage, args[2], args[3]));
499+
return Tuple.of(cred, Blob.load(storage, args[2], args[3]));
519500
}
520501

521502
@Override

gcloud-java-storage/src/main/java/com/google/gcloud/storage/Blob.java

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -109,28 +109,27 @@ public Blob(Storage storage, BlobInfo info) {
109109
}
110110

111111
/**
112-
* Constructs a {@code Blob} object for the provided bucket and blob names. The storage service is
113-
* used to issue requests.
112+
* Creates a {@code Blob} object for the provided bucket and blob names. Performs an RPC call to
113+
* get the latest blob information.
114114
*
115115
* @param storage the storage service used for issuing requests
116116
* @param bucket bucket's name
117117
* @param blob blob's name
118118
*/
119-
public Blob(Storage storage, String bucket, String blob) {
120-
this.storage = checkNotNull(storage);
121-
this.info = BlobInfo.builder(BlobId.of(bucket, blob)).build();
119+
public static Blob load(Storage storage, String bucket, String blob) {
120+
return load(storage, BlobId.of(bucket, blob));
122121
}
123122

124123
/**
125-
* Constructs a {@code Blob} object for the provided {@code BlobId}. The storage service is used
126-
* to issue requests.
124+
* Creates a {@code Blob} object for the provided {@code blobId}. Performs an RPC call to get the
125+
* latest blob information.
127126
*
128127
* @param storage the storage service used for issuing requests
129128
* @param blobId blob's identifier
130129
*/
131-
public Blob(Storage storage, BlobId blobId) {
132-
this.storage = checkNotNull(storage);
133-
this.info = BlobInfo.builder(blobId).build();
130+
public static Blob load(Storage storage, BlobId blobId) {
131+
BlobInfo info = storage.get(blobId);
132+
return new Blob(storage, info);
134133
}
135134

136135
/**
@@ -209,9 +208,8 @@ public Blob update(BlobInfo blobInfo, BlobTargetOption... options) {
209208
*/
210209
public Blob copyTo(BlobId targetBlob, BlobSourceOption... options) {
211210
BlobInfo updatedInfo = info.toBuilder().blobId(targetBlob).build();
212-
CopyRequest copyRequest =
213-
CopyRequest.builder().source(info.bucket(), info.name())
214-
.sourceOptions(convert(info, options)).target(updatedInfo).build();
211+
CopyRequest copyRequest = CopyRequest.builder().source(info.bucket(), info.name())
212+
.sourceOptions(convert(info, options)).target(updatedInfo).build();
215213
return new Blob(storage, storage.copy(copyRequest));
216214
}
217215

@@ -251,9 +249,8 @@ public Blob copyTo(String targetBucket, BlobSourceOption... options) {
251249
*/
252250
public Blob copyTo(String targetBucket, String targetBlob, BlobSourceOption... options) {
253251
BlobInfo updatedInfo = info.toBuilder().blobId(BlobId.of(targetBucket, targetBlob)).build();
254-
CopyRequest copyRequest =
255-
CopyRequest.builder().source(info.bucket(), info.name())
256-
.sourceOptions(convert(info, options)).target(updatedInfo).build();
252+
CopyRequest copyRequest = CopyRequest.builder().source(info.bucket(), info.name())
253+
.sourceOptions(convert(info, options)).target(updatedInfo).build();
257254
return new Blob(storage, storage.copy(copyRequest));
258255
}
259256

gcloud-java-storage/src/main/java/com/google/gcloud/storage/Bucket.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,15 @@ public Bucket(Storage storage, BucketInfo info) {
5656
}
5757

5858
/**
59-
* Constructs a {@code Bucket} object for the provided name. The storage service is used to issue
60-
* requests.
59+
* Creates a {@code Bucket} object for the provided bucket name. Performs an RPC call to get the
60+
* latest bucket information.
6161
*
6262
* @param storage the storage service used for issuing requests
6363
* @param bucket bucket's name
6464
*/
65-
public Bucket(Storage storage, String bucket) {
66-
this.storage = checkNotNull(storage);
67-
this.info = BucketInfo.of(checkNotNull(bucket));
65+
public static Bucket load(Storage storage, String bucket) {
66+
BucketInfo info = storage.get(bucket);
67+
return new Bucket(storage, info);
6868
}
6969

7070
/**

gcloud-java-storage/src/test/java/com/google/gcloud/storage/BlobTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,4 +271,20 @@ public void testDeleteSome() throws Exception {
271271
assertEquals(deleleResultList.get(i), result.get(i));
272272
}
273273
}
274+
275+
@Test
276+
public void testLoadFromString() throws Exception {
277+
expect(storage.get(BLOB_INFO.blobId())).andReturn(BLOB_INFO);
278+
replay(storage);
279+
Blob loadedBlob = Blob.load(storage, BLOB_INFO.bucket(), BLOB_INFO.name());
280+
assertEquals(BLOB_INFO, loadedBlob.info());
281+
}
282+
283+
@Test
284+
public void testLoadFromId() throws Exception {
285+
expect(storage.get(BLOB_INFO.blobId())).andReturn(BLOB_INFO);
286+
replay(storage);
287+
Blob loadedBlob = Blob.load(storage, BLOB_INFO.blobId());
288+
assertEquals(BLOB_INFO, loadedBlob.info());
289+
}
274290
}

gcloud-java-storage/src/test/java/com/google/gcloud/storage/BucketTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,4 +168,12 @@ public void testCreate() throws Exception {
168168
Blob blob = bucket.create("n", content);
169169
assertEquals(info, blob.info());
170170
}
171+
172+
@Test
173+
public void testLoad() throws Exception {
174+
expect(storage.get(BUCKET_INFO.name())).andReturn(BUCKET_INFO);
175+
replay(storage);
176+
Bucket loadedBucket = Bucket.load(storage, BUCKET_INFO.name());
177+
assertEquals(BUCKET_INFO, loadedBucket.info());
178+
}
171179
}

0 commit comments

Comments
 (0)