Skip to content

Commit 5227abc

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
1 parent cedc9a2 commit 5227abc

20 files changed

+465
-243
lines changed

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: 34 additions & 16 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;
@@ -118,7 +117,19 @@ public Blob(Storage storage, BlobInfo info) {
118117
*/
119118
public Blob(Storage storage, String bucket, String blob) {
120119
this.storage = checkNotNull(storage);
121-
this.info = BlobInfo.of(checkNotNull(bucket), checkNotNull(blob));
120+
this.info = BlobInfo.builder(BlobId.of(bucket, blob)).build();
121+
}
122+
123+
/**
124+
* Constructs a {@code Blob} object for the provided {@code BlobId}. The storage service is used
125+
* to issue requests.
126+
*
127+
* @param storage the storage service used for issuing requests
128+
* @param blobId blob's identifier
129+
*/
130+
public Blob(Storage storage, BlobId blobId) {
131+
this.storage = checkNotNull(storage);
132+
this.info = BlobInfo.builder(blobId).build();
122133
}
123134

124135
/**
@@ -128,6 +139,13 @@ public BlobInfo info() {
128139
return info;
129140
}
130141

142+
/**
143+
* Returns the blob's id.
144+
*/
145+
public BlobId id() {
146+
return info.blobId();
147+
}
148+
131149
/**
132150
* Checks if this blob exists.
133151
*
@@ -213,7 +231,7 @@ public Blob copyTo(String targetBucket, BlobSourceOption... options) {
213231
* @throws StorageException upon failure
214232
*/
215233
public Blob copyTo(String targetBucket, String targetBlob, BlobSourceOption... options) {
216-
BlobInfo updatedInfo = info.toBuilder().bucket(targetBucket).name(targetBlob).build();
234+
BlobInfo updatedInfo = info.toBuilder().blobId(BlobId.of(targetBucket, targetBlob)).build();
217235
CopyRequest copyRequest =
218236
CopyRequest.builder().source(info.bucket(), info.name())
219237
.sourceOptions(convert(info, options)).target(updatedInfo).build();
@@ -267,18 +285,18 @@ public Storage storage() {
267285
* {@code infos.length > 1} a batch request is used to fetch blobs.
268286
*
269287
* @param storage the storage service used to issue the request
270-
* @param infos the blobs to get
288+
* @param blobs the blobs to get
271289
* @return an immutable list of {@code Blob} objects. If a blob does not exist or access to it has
272-
* been denied the corresponding item in the list is {@code null}.
290+
* been denied the corresponding item in the list is {@code null}.
273291
* @throws StorageException upon failure
274292
*/
275-
public static List<Blob> get(final Storage storage, BlobInfo... infos) {
293+
public static List<Blob> get(final Storage storage, BlobId... blobs) {
276294
checkNotNull(storage);
277-
checkNotNull(infos);
278-
if (infos.length == 0) {
295+
checkNotNull(blobs);
296+
if (blobs.length == 0) {
279297
return Collections.emptyList();
280298
}
281-
return Collections.unmodifiableList(Lists.transform(storage.get(infos),
299+
return Collections.unmodifiableList(Lists.transform(storage.get(blobs),
282300
new Function<BlobInfo, Blob>() {
283301
@Override
284302
public Blob apply(BlobInfo f) {
@@ -294,7 +312,7 @@ public Blob apply(BlobInfo f) {
294312
* @param storage the storage service used to issue the request
295313
* @param infos the blobs to update
296314
* @return an immutable list of {@code Blob} objects. If a blob does not exist or access to it has
297-
* been denied the corresponding item in the list is {@code null}.
315+
* been denied the corresponding item in the list is {@code null}.
298316
* @throws StorageException upon failure
299317
*/
300318
public static List<Blob> update(final Storage storage, BlobInfo... infos) {
@@ -317,18 +335,18 @@ public Blob apply(BlobInfo f) {
317335
* {@code infos.length > 1} a batch request is used to delete blobs.
318336
*
319337
* @param storage the storage service used to issue the request
320-
* @param infos the blobs to delete
338+
* @param blobs the blobs to delete
321339
* @return an immutable list of booleans. If a blob has been deleted the corresponding item in the
322-
* list is {@code true}. If deletion failed or access to the resource was denied the item is
340+
* list is {@code true}. If deletion failed or access to the resource was denied the item is
323341
* {@code false}.
324342
* @throws StorageException upon failure
325343
*/
326-
public static List<Boolean> delete(Storage storage, BlobInfo... infos) {
344+
public static List<Boolean> delete(Storage storage, BlobId... blobs) {
327345
checkNotNull(storage);
328-
checkNotNull(infos);
329-
if (infos.length == 0) {
346+
checkNotNull(blobs);
347+
if (blobs.length == 0) {
330348
return Collections.emptyList();
331349
}
332-
return storage.delete(infos);
350+
return storage.delete(blobs);
333351
}
334352
}
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)