Skip to content

Commit 264a8ae

Browse files
committed
Add BlobId class and update other storage classes accordingly
- add storage.get(BlobId blob, BlobSourceOption... options) - add storage.get(BlobId blob) - add storage.delete(BlobId blob, BlobSourceOption... options) - add storage.readAllBytes(BlobId blob, BlobSourceOption... options) - add storage.reader(BlobId blob, BlobSourceOption... options) - refactor CopyRequest to take BlobId as source - refactor BatchRequest to take BlobId for get/delete - change batch get to get(BlobId... blobIds) - change batch delete to delete(BlobId... blobIds) - update tests - update examples
1 parent 52e60ee commit 264a8ae

21 files changed

Lines changed: 464 additions & 242 deletions

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,7 @@ CopyRequest parse(Storage storage, String... args) {
393393
if (args.length != 4) {
394394
throw new IllegalArgumentException();
395395
}
396-
return CopyRequest.of(args[0], args[1], BlobInfo.of(args[2], args[3]));
396+
return CopyRequest.of(args[0], args[1], BlobInfo.builder(args[2], args[3]).build());
397397
}
398398

399399
@Override
@@ -420,7 +420,7 @@ ComposeRequest parse(Storage storage, String... args) {
420420
throw new IllegalArgumentException();
421421
}
422422
ComposeRequest.Builder request = ComposeRequest.builder();
423-
request.target(BlobInfo.of(args[0], args[args.length - 1]));
423+
request.target(BlobInfo.builder(args[0], args[args.length - 1]).build());
424424
for (int i = 1; i < args.length - 1; i++) {
425425
request.addSource(args[i]);
426426
}

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

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,23 +33,31 @@ public final class BatchRequest implements Serializable {
3333

3434
private static final long serialVersionUID = -1527992265939800345L;
3535

36-
private final Map<BlobInfo, Iterable<BlobSourceOption>> toDelete;
36+
private final Map<BlobId, Iterable<BlobSourceOption>> toDelete;
3737
private final Map<BlobInfo, Iterable<BlobTargetOption>> toUpdate;
38-
private final Map<BlobInfo, Iterable<BlobSourceOption>> toGet;
38+
private final Map<BlobId, Iterable<BlobSourceOption>> toGet;
3939

4040
public static class Builder {
4141

42-
private Map<BlobInfo, Iterable<BlobSourceOption>> toDelete = new LinkedHashMap<>();
42+
private Map<BlobId, Iterable<BlobSourceOption>> toDelete = new LinkedHashMap<>();
4343
private Map<BlobInfo, Iterable<BlobTargetOption>> toUpdate = new LinkedHashMap<>();
44-
private Map<BlobInfo, Iterable<BlobSourceOption>> toGet = new LinkedHashMap<>();
44+
private Map<BlobId, Iterable<BlobSourceOption>> toGet = new LinkedHashMap<>();
4545

4646
private Builder() {}
4747

4848
/**
4949
* Delete the given blob.
5050
*/
5151
public Builder delete(String bucket, String blob, BlobSourceOption... options) {
52-
toDelete.put(BlobInfo.of(bucket, blob), Lists.newArrayList(options));
52+
toDelete.put(BlobId.of(bucket, blob), Lists.newArrayList(options));
53+
return this;
54+
}
55+
56+
/**
57+
* Delete the given blob.
58+
*/
59+
public Builder delete(BlobId blob, BlobSourceOption... options) {
60+
toDelete.put(blob, Lists.newArrayList(options));
5361
return this;
5462
}
5563

@@ -65,7 +73,15 @@ public Builder update(BlobInfo blobInfo, BlobTargetOption... options) {
6573
* Retrieve metadata for the given blob.
6674
*/
6775
public Builder get(String bucket, String blob, BlobSourceOption... options) {
68-
toGet.put(BlobInfo.of(bucket, blob), Lists.newArrayList(options));
76+
toGet.put(BlobId.of(bucket, blob), Lists.newArrayList(options));
77+
return this;
78+
}
79+
80+
/**
81+
* Retrieve metadata for the given blob.
82+
*/
83+
public Builder get(BlobId blob, BlobSourceOption... options) {
84+
toGet.put(blob, Lists.newArrayList(options));
6985
return this;
7086
}
7187

@@ -96,15 +112,15 @@ public boolean equals(Object obj) {
96112
&& Objects.equals(toGet, other.toGet);
97113
}
98114

99-
public Map<BlobInfo, Iterable<BlobSourceOption>> toDelete() {
115+
public Map<BlobId, Iterable<BlobSourceOption>> toDelete() {
100116
return toDelete;
101117
}
102118

103119
public Map<BlobInfo, Iterable<BlobTargetOption>> toUpdate() {
104120
return toUpdate;
105121
}
106122

107-
public Map<BlobInfo, Iterable<BlobSourceOption>> toGet() {
123+
public Map<BlobId, Iterable<BlobSourceOption>> toGet() {
108124
return toGet;
109125
}
110126

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

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import com.google.gcloud.storage.Storage.SignUrlOption;
2929

3030
import java.net.URL;
31-
import java.util.Arrays;
3231
import java.util.Collections;
3332
import java.util.List;
3433
import java.util.Objects;
@@ -119,7 +118,19 @@ public Blob(Storage storage, BlobInfo info) {
119118
*/
120119
public Blob(Storage storage, String bucket, String blob) {
121120
this.storage = checkNotNull(storage);
122-
this.info = BlobInfo.of(checkNotNull(bucket), checkNotNull(blob));
121+
this.info = BlobInfo.builder(BlobId.of(bucket, blob)).build();
122+
}
123+
124+
/**
125+
* Constructs a {@code Blob} object for the provided {@code BlobId}. The storage service is used
126+
* to issue requests.
127+
*
128+
* @param storage the storage service used for issuing requests
129+
* @param blobId blob's identifier
130+
*/
131+
public Blob(Storage storage, BlobId blobId) {
132+
this.storage = checkNotNull(storage);
133+
this.info = BlobInfo.builder(blobId).build();
123134
}
124135

125136
/**
@@ -129,6 +140,13 @@ public BlobInfo info() {
129140
return info;
130141
}
131142

143+
/**
144+
* Returns the blob's id.
145+
*/
146+
public BlobId id() {
147+
return info.blobId();
148+
}
149+
132150
/**
133151
* Checks if this blob exists.
134152
*
@@ -214,7 +232,7 @@ public Blob copyTo(String targetBucket, BlobSourceOption... options) {
214232
* @throws StorageException upon failure
215233
*/
216234
public Blob copyTo(String targetBucket, String targetBlob, BlobSourceOption... options) {
217-
BlobInfo updatedInfo = info.toBuilder().bucket(targetBucket).name(targetBlob).build();
235+
BlobInfo updatedInfo = info.toBuilder().blobId(BlobId.of(targetBucket, targetBlob)).build();
218236
CopyRequest copyRequest =
219237
CopyRequest.builder().source(info.bucket(), info.name())
220238
.sourceOptions(convert(info, options)).target(updatedInfo).build();
@@ -270,18 +288,18 @@ public Storage storage() {
270288
* {@code infos.length > 1} a batch request is used to fetch blobs.
271289
*
272290
* @param storage the storage service used to issue the request
273-
* @param infos the blobs to get
291+
* @param blobs the blobs to get
274292
* @return an immutable list of {@code Blob} objects. If a blob does not exist or access to it has
275293
* been denied the corresponding item in the list is {@code null}.
276294
* @throws StorageException upon failure
277295
*/
278-
public static List<Blob> get(final Storage storage, BlobInfo... infos) {
296+
public static List<Blob> get(final Storage storage, BlobId... blobs) {
279297
checkNotNull(storage);
280-
checkNotNull(infos);
281-
if (infos.length == 0) {
298+
checkNotNull(blobs);
299+
if (blobs.length == 0) {
282300
return Collections.emptyList();
283301
}
284-
return Collections.unmodifiableList(Lists.transform(storage.get(infos),
302+
return Collections.unmodifiableList(Lists.transform(storage.get(blobs),
285303
new Function<BlobInfo, Blob>() {
286304
@Override
287305
public Blob apply(BlobInfo f) {
@@ -320,18 +338,18 @@ public Blob apply(BlobInfo f) {
320338
* {@code infos.length > 1} a batch request is used to delete blobs.
321339
*
322340
* @param storage the storage service used to issue the request
323-
* @param infos the blobs to delete
341+
* @param blobs the blobs to delete
324342
* @return an immutable list of booleans. If a blob has been deleted the corresponding item in the
325343
* list is {@code true}. If deletion failed or access to the resource was denied the item is
326344
* {@code false}.
327345
* @throws StorageException upon failure
328346
*/
329-
public static List<Boolean> delete(Storage storage, BlobInfo... infos) {
347+
public static List<Boolean> delete(Storage storage, BlobId... blobs) {
330348
checkNotNull(storage);
331-
checkNotNull(infos);
332-
if (infos.length == 0) {
349+
checkNotNull(blobs);
350+
if (blobs.length == 0) {
333351
return Collections.emptyList();
334352
}
335-
return storage.delete(infos);
353+
return storage.delete(blobs);
336354
}
337355
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* Copyright 2015 Google Inc. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.gcloud.storage;
18+
19+
import com.google.api.services.storage.model.StorageObject;
20+
import static com.google.common.base.Preconditions.checkNotNull;
21+
22+
import com.google.common.base.MoreObjects;
23+
24+
import java.io.Serializable;
25+
import java.util.Objects;
26+
27+
/**
28+
* Google Storage object identifier.
29+
*/
30+
public final class BlobId implements Serializable {
31+
32+
private static final long serialVersionUID = -6156002883225601925L;
33+
private final String bucket;
34+
private final String name;
35+
36+
private BlobId(String bucket, String name) {
37+
this.bucket = bucket;
38+
this.name = name;
39+
}
40+
41+
public String bucket() {
42+
return bucket;
43+
}
44+
45+
public String name() {
46+
return name;
47+
}
48+
49+
@Override
50+
public String toString() {
51+
return MoreObjects.toStringHelper(this)
52+
.add("bucket", bucket())
53+
.add("name", name())
54+
.toString();
55+
}
56+
57+
@Override
58+
public int hashCode() {
59+
return Objects.hash(bucket, name);
60+
}
61+
62+
@Override
63+
public boolean equals(Object obj) {
64+
return obj instanceof BlobId && Objects.equals(bucket, ((BlobId) obj).bucket)
65+
&& Objects.equals(name, ((BlobId) obj).name);
66+
}
67+
68+
StorageObject toPb() {
69+
StorageObject storageObject = new StorageObject();
70+
storageObject.setBucket(bucket);
71+
storageObject.setName(name);
72+
return storageObject;
73+
}
74+
75+
public static BlobId of(String bucket, String name) {
76+
return new BlobId(checkNotNull(bucket), checkNotNull(name));
77+
}
78+
79+
static BlobId fromPb(StorageObject storageObject) {
80+
return BlobId.of(storageObject.getBucket(), storageObject.getName());
81+
}
82+
}

0 commit comments

Comments
 (0)