Skip to content

Commit 5e1d71b

Browse files
committed
Merge pull request #251 from mziccard/add-blob-id
Add BlobId class to identify a storage object
2 parents 52e60ee + 41da3aa commit 5e1d71b

File tree

21 files changed

+519
-253
lines changed

21 files changed

+519
-253
lines changed

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: 53 additions & 18 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,14 +140,21 @@ 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
*
135153
* @return true if this blob exists, false otherwise
136154
* @throws StorageException upon failure
137155
*/
138156
public boolean exists() {
139-
return storage.get(info.bucket(), info.name()) != null;
157+
return storage.get(info.blobId()) != null;
140158
}
141159

142160
/**
@@ -146,7 +164,7 @@ public boolean exists() {
146164
* @throws StorageException upon failure
147165
*/
148166
public byte[] content(Storage.BlobSourceOption... options) {
149-
return storage.readAllBytes(info.bucket(), info.name(), options);
167+
return storage.readAllBytes(info.blobId(), options);
150168
}
151169

152170
/**
@@ -157,7 +175,7 @@ public byte[] content(Storage.BlobSourceOption... options) {
157175
* @throws StorageException upon failure
158176
*/
159177
public Blob reload(BlobSourceOption... options) {
160-
return new Blob(storage, storage.get(info.bucket(), info.name(), convert(info, options)));
178+
return new Blob(storage, storage.get(info.blobId(), convert(info, options)));
161179
}
162180

163181
/**
@@ -179,6 +197,23 @@ public Blob update(BlobInfo blobInfo, BlobTargetOption... options) {
179197
return new Blob(storage, storage.update(blobInfo, options));
180198
}
181199

200+
/**
201+
* Copies this blob to the specified target. Possibly copying also some of the metadata
202+
* (e.g. content-type).
203+
*
204+
* @param targetBlob target blob's id
205+
* @param options source blob options
206+
* @return the copied blob
207+
* @throws StorageException upon failure
208+
*/
209+
public Blob copyTo(BlobId targetBlob, BlobSourceOption... options) {
210+
BlobInfo updatedInfo = info.toBuilder().blobId(targetBlob).build();
211+
CopyRequest copyRequest =
212+
CopyRequest.builder().source(info.bucket(), info.name())
213+
.sourceOptions(convert(info, options)).target(updatedInfo).build();
214+
return new Blob(storage, storage.copy(copyRequest));
215+
}
216+
182217
/**
183218
* Deletes this blob.
184219
*
@@ -187,7 +222,7 @@ public Blob update(BlobInfo blobInfo, BlobTargetOption... options) {
187222
* @throws StorageException upon failure
188223
*/
189224
public boolean delete(BlobSourceOption... options) {
190-
return storage.delete(info.bucket(), info.name(), convert(info, options));
225+
return storage.delete(info.blobId(), convert(info, options));
191226
}
192227

193228
/**
@@ -214,7 +249,7 @@ public Blob copyTo(String targetBucket, BlobSourceOption... options) {
214249
* @throws StorageException upon failure
215250
*/
216251
public Blob copyTo(String targetBucket, String targetBlob, BlobSourceOption... options) {
217-
BlobInfo updatedInfo = info.toBuilder().bucket(targetBucket).name(targetBlob).build();
252+
BlobInfo updatedInfo = info.toBuilder().blobId(BlobId.of(targetBucket, targetBlob)).build();
218253
CopyRequest copyRequest =
219254
CopyRequest.builder().source(info.bucket(), info.name())
220255
.sourceOptions(convert(info, options)).target(updatedInfo).build();
@@ -228,7 +263,7 @@ public Blob copyTo(String targetBucket, String targetBlob, BlobSourceOption... o
228263
* @throws StorageException upon failure
229264
*/
230265
public BlobReadChannel reader(BlobSourceOption... options) {
231-
return storage.reader(info.bucket(), info.name(), convert(info, options));
266+
return storage.reader(info.blobId(), convert(info, options));
232267
}
233268

234269
/**
@@ -270,18 +305,18 @@ public Storage storage() {
270305
* {@code infos.length > 1} a batch request is used to fetch blobs.
271306
*
272307
* @param storage the storage service used to issue the request
273-
* @param infos the blobs to get
308+
* @param blobs the blobs to get
274309
* @return an immutable list of {@code Blob} objects. If a blob does not exist or access to it has
275310
* been denied the corresponding item in the list is {@code null}.
276311
* @throws StorageException upon failure
277312
*/
278-
public static List<Blob> get(final Storage storage, BlobInfo... infos) {
313+
public static List<Blob> get(final Storage storage, BlobId... blobs) {
279314
checkNotNull(storage);
280-
checkNotNull(infos);
281-
if (infos.length == 0) {
315+
checkNotNull(blobs);
316+
if (blobs.length == 0) {
282317
return Collections.emptyList();
283318
}
284-
return Collections.unmodifiableList(Lists.transform(storage.get(infos),
319+
return Collections.unmodifiableList(Lists.transform(storage.get(blobs),
285320
new Function<BlobInfo, Blob>() {
286321
@Override
287322
public Blob apply(BlobInfo f) {
@@ -320,18 +355,18 @@ public Blob apply(BlobInfo f) {
320355
* {@code infos.length > 1} a batch request is used to delete blobs.
321356
*
322357
* @param storage the storage service used to issue the request
323-
* @param infos the blobs to delete
358+
* @param blobs the blobs to delete
324359
* @return an immutable list of booleans. If a blob has been deleted the corresponding item in the
325360
* list is {@code true}. If deletion failed or access to the resource was denied the item is
326361
* {@code false}.
327362
* @throws StorageException upon failure
328363
*/
329-
public static List<Boolean> delete(Storage storage, BlobInfo... infos) {
364+
public static List<Boolean> delete(Storage storage, BlobId... blobs) {
330365
checkNotNull(storage);
331-
checkNotNull(infos);
332-
if (infos.length == 0) {
366+
checkNotNull(blobs);
367+
if (blobs.length == 0) {
333368
return Collections.emptyList();
334369
}
335-
return storage.delete(infos);
370+
return storage.delete(blobs);
336371
}
337372
}
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)