Skip to content

Commit 44cefb0

Browse files
committed
Blob and Bucket extended with more functionalities, added tests
1 parent d8fa7bb commit 44cefb0

File tree

4 files changed

+313
-28
lines changed

4 files changed

+313
-28
lines changed

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

Lines changed: 59 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
/*
32
* Copyright 2015 Google Inc. All Rights Reserved.
43
*
@@ -31,7 +30,6 @@
3130
import java.net.URL;
3231
import java.util.Objects;
3332

34-
3533
/**
3634
* A Google cloud storage object.
3735
*/
@@ -89,38 +87,68 @@ static Storage.BlobSourceOption[] convert(BlobInfo blobInfo, BlobSourceOption...
8987
}
9088
}
9189

90+
/**
91+
* Construct a {@code Blob} object for the provided {@code BlobInfo}. The storage service is used
92+
* to issue requests.
93+
*
94+
* @param storage the storage service used for issuing requests
95+
* @param info blob's info
96+
*/
9297
public Blob(Storage storage, BlobInfo info) {
9398
this.storage = checkNotNull(storage);
9499
this.info = checkNotNull(info);
95100
}
96101

102+
/**
103+
* Construct a {@code Blob} object for the provided bucket and blob names. The storage service is
104+
* used to issue requests.
105+
*
106+
* @param storage the storage service used for issuing requests
107+
* @param bucket bucket's name
108+
* @param blob blob's name
109+
*/
110+
public Blob(Storage storage, String bucket, String blob) {
111+
this.storage = checkNotNull(storage);
112+
this.info = BlobInfo.of(bucket, blob);
113+
}
114+
115+
/**
116+
* Get the blobs's information.
117+
*
118+
* @return a {@code BlobInfo} object for this blob
119+
*/
97120
public BlobInfo info() {
98121
return info;
99122
}
100123

101124
/**
102-
* Returns true if this blob exists.
125+
* Check if this blob exists.
103126
*
127+
* @return true if this blob exists, false otherwise
104128
* @throws StorageException upon failure
105129
*/
106130
public boolean exists() {
107131
return storage.get(info.bucket(), info.name()) != null;
108132
}
109133

110134
/**
111-
* Returns the blob's content.
135+
* Return this blob's content.
112136
*
137+
* @param options blob read options
138+
* @return the blob's content
113139
* @throws StorageException upon failure
114140
*/
115141
public byte[] content(Storage.BlobSourceOption... options) {
116142
return storage.readAllBytes(info.bucket(), info.name(), options);
117143
}
118144

119145
/**
120-
* Updates the blob's information. Bucket or blob's name cannot be changed by this method. If you
146+
* Update the blob's information. Bucket or blob's name cannot be changed by this method. If you
121147
* want to rename the blob or move it to a different bucket use the {@link #copyTo} and
122148
* {@link #delete} operations.
123149
*
150+
* @param blobInfo new blob's information. Bucket and blob names must match the current ones
151+
* @param options update options
124152
* @throws StorageException upon failure
125153
*/
126154
public void update(BlobInfo blobInfo, BlobTargetOption... options) {
@@ -130,29 +158,35 @@ public void update(BlobInfo blobInfo, BlobTargetOption... options) {
130158
}
131159

132160
/**
133-
* Deletes this blob.
161+
* Delete this blob.
134162
*
135-
* @return true if bucket was deleted
163+
* @param options blob delete options
164+
* @return true if blob was deleted
136165
* @throws StorageException upon failure
137166
*/
138167
public boolean delete(BlobSourceOption... options) {
139168
return storage.delete(info.bucket(), info.name(), convert(info, options));
140169
}
141170

142171
/**
143-
* Send a copy request.
172+
* Copy this blob.
144173
*
145-
* @return the copied blob.
174+
* @param target target blob
175+
* @param options source blob options
176+
* @return the copied blob
146177
* @throws StorageException upon failure
147178
*/
148179
public Blob copyTo(BlobInfo target, BlobSourceOption... options) {
149180
return copyTo(target, ImmutableList.copyOf(options), ImmutableList.<BlobTargetOption>of());
150181
}
151182

152183
/**
153-
* Send a copy request.
184+
* Copy this blob.
154185
*
155-
* @return the copied blob.
186+
* @param target target blob
187+
* @param sourceOptions source blob options
188+
* @param targetOptions target blob options
189+
* @return the copied blob
156190
* @throws StorageException upon failure
157191
*/
158192
public Blob copyTo(BlobInfo target, Iterable<BlobSourceOption> sourceOptions,
@@ -165,36 +199,47 @@ public Blob copyTo(BlobInfo target, Iterable<BlobSourceOption> sourceOptions,
165199
}
166200

167201
/**
168-
* Returns a channel for reading this blob's content.
202+
* Return a channel for reading this blob's content.
169203
*
204+
* @param options blob read options
205+
* @return a {@code BlobReadChannel} object to read this blob
170206
* @throws StorageException upon failure
171207
*/
172208
public BlobReadChannel reader(BlobSourceOption... options) {
173209
return storage.reader(info.bucket(), info.name(), convert(info, options));
174210
}
175211

176212
/**
177-
* Returns a channel for writing to this blob.
213+
* Return a channel for writing to this blob.
178214
*
215+
* @param options target blob options
216+
* @return a {@code BlobWriteChannel} object for writing to this blob
179217
* @throws StorageException upon failure
180218
*/
181219
public BlobWriteChannel writer(BlobTargetOption... options) {
182220
return storage.writer(info, options);
183221
}
184222

185223
/**
186-
* Generates a signed URL for this blob. If you want to allow access to for a fixed amount of time
224+
* Generate a signed URL for this blob. If you want to allow access to for a fixed amount of time
187225
* for this blob, you can use this method to generate a URL that is only valid within a certain
188226
* time period. This is particularly useful if you don't want publicly accessible blobs, but don't
189227
* want to require users to explicitly log in.
190228
*
191229
* @param expirationTimeInSeconds the signed URL expiration (using epoch time)
230+
* @param options signed url options
231+
* @return a signed URL for this bucket and the specified options
192232
* @see <a href="https://cloud.google.com/storage/docs/access-control#Signed-URLs">Signed-URLs</a>
193233
*/
194234
public URL signUrl(long expirationTimeInSeconds, SignUrlOption... options) {
195235
return storage.signUrl(info, expirationTimeInSeconds, options);
196236
}
197237

238+
/**
239+
* Get this blobs's {@code Storage} object.
240+
*
241+
* @return the storage service used by this blob to issue requests
242+
*/
198243
public Storage storage() {
199244
return storage;
200245
}

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

Lines changed: 90 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@
2020
import static com.google.common.base.Preconditions.checkNotNull;
2121

2222
import com.google.gcloud.storage.Storage.BlobSourceOption;
23+
import com.google.gcloud.storage.Storage.BlobTargetOption;
2324
import com.google.gcloud.storage.Storage.BucketSourceOption;
2425
import com.google.gcloud.storage.Storage.BucketTargetOption;
26+
import java.util.LinkedList;
2527

2628
import java.util.List;
2729
import java.util.Objects;
@@ -34,14 +36,43 @@ public final class Bucket {
3436
private final Storage storage;
3537
private BucketInfo info;
3638

39+
/**
40+
* Construct a {@code Bucket} object for the provided {@code BucketInfo}. The storage service is
41+
* used to issue requests.
42+
*
43+
* @param storage the storage service used for issuing requests
44+
* @param info bucket's info
45+
*/
3746
public Bucket(Storage storage, BucketInfo info) {
3847
this.storage = checkNotNull(storage);
3948
this.info = checkNotNull(info);
4049
}
4150

4251
/**
43-
* Returns true if this bucket exists.
52+
* Construct a {@code Bucket} object for the provided name. The storage service is used to issue
53+
* requests.
54+
*
55+
* @param storage the storage service used for issuing requests
56+
* @param bucket bucket's name
57+
*/
58+
public Bucket(Storage storage, String bucket) {
59+
this.storage = checkNotNull(storage);
60+
this.info = BucketInfo.of(bucket);
61+
}
62+
63+
/**
64+
* Get the bucket's information.
65+
*
66+
* @return a {@code BucketInfo} object for this bucket
67+
*/
68+
public BucketInfo info() {
69+
return info;
70+
}
71+
72+
/**
73+
* Check if this bucket exists.
4474
*
75+
* @return true if this bucket exists, false otherwise
4576
* @throws StorageException upon failure
4677
*/
4778
public boolean exists() {
@@ -51,6 +82,8 @@ public boolean exists() {
5182
/**
5283
* Update the bucket's information. Bucket's name cannot be changed.
5384
*
85+
* @param bucketInfo new bucket's information. Name must match the one of the current bucket
86+
* @param options update options
5487
* @throws StorageException upon failure
5588
*/
5689
public void update(BucketInfo bucketInfo, BucketTargetOption... options) {
@@ -61,33 +94,77 @@ public void update(BucketInfo bucketInfo, BucketTargetOption... options) {
6194
/**
6295
* Delete this bucket.
6396
*
97+
* @param options bucket delete options
6498
* @return true if bucket was deleted
6599
* @throws StorageException upon failure
66100
*/
67101
public boolean delete(BucketSourceOption... options) {
68102
return storage.delete(info.name(), options);
69103
}
70104

71-
public ListResult<BlobInfo> list(Storage.BlobListOption... options) {
72-
return storage.list(info.name(), options);
105+
/**
106+
* List blobs in this bucket.
107+
*
108+
* @param options options for listing blobs
109+
* @return the paginated list of {@code Blob} objects in this bucket
110+
* @throws StorageException upon failure
111+
*/
112+
public ListResult<Blob> list(Storage.BlobListOption... options) {
113+
return new BlobListResult(storage, storage.list(info.name(), options));
73114
}
74115

75-
public BlobInfo get(String blob, BlobSourceOption... options) {
76-
return null;
116+
/**
117+
* Return the requested blob in this bucket or {@code null} if not found.
118+
*
119+
* @param blob name of the requested blob
120+
* @param options blob search options
121+
* @return the requested blob in this bucket or {@code null} if not found
122+
* @throws StorageException upon failure
123+
*/
124+
public Blob get(String blob, BlobSourceOption... options) {
125+
return new Blob(storage, storage.get(info.name(), blob, options));
77126
}
78127

79-
public List<Blob> get(String... blob) {
80-
// todo
81-
return null;
128+
/**
129+
* Return a list of requested blobs in this bucket. Blobs that do not exist are null.
130+
*
131+
* @param blobNames names of the requested blobs
132+
* @return a list containing the requested blobs in this bucket
133+
* @throws StorageException upon failure
134+
*/
135+
public List<Blob> getAll(String... blobNames) {
136+
BatchRequest.Builder batch = BatchRequest.builder();
137+
for (String blobName : blobNames) {
138+
batch.get(info.name(), blobName);
139+
}
140+
List<Blob> blobs = new LinkedList<>();
141+
BatchResponse response = storage.apply(batch.build());
142+
for (BatchResponse.Result<BlobInfo> result : response.gets()) {
143+
BlobInfo blobInfo = result.get();
144+
blobs.add(blobInfo != null ? new Blob(storage, blobInfo) : null);
145+
}
146+
return blobs;
82147
}
83148

84-
/*
85-
* BlobInfo create(BlobInfo blobInfo, byte[] content, BlobTargetOption... options) {
86-
*
87-
* }
149+
/**
150+
* Create a new blob in this bucket.
151+
*
152+
* @param blob a blob name
153+
* @param content the blob content
154+
* @param options options for blob creation
155+
* @return a complete blob information.
156+
* @throws StorageException upon failure
88157
*/
158+
Blob create(String blob, byte[] content, BlobTargetOption... options) {
159+
BlobInfo blobInfo = BlobInfo.of(info.name(), blob);
160+
return new Blob(storage, storage.create(blobInfo, content, options));
161+
}
89162

90-
163+
/**
164+
* Get this bucket's {@code Storage} object.
165+
*
166+
* @return the storage service used by this bucket to issue requests
167+
*/
91168
public Storage storage() {
92169
return storage;
93170
}

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public class BlobTest {
4141

4242
private Storage storage;
4343
private Blob blob;
44-
private BlobInfo blobInfo = BlobInfo.of("b", "n");
44+
private final BlobInfo blobInfo = BlobInfo.of("b", "n");
4545

4646
@Before
4747
public void setUp() throws Exception {
@@ -107,6 +107,9 @@ public void testCopyTo() throws Exception {
107107
replay(storage);
108108
Blob targetBlob = blob.copyTo(target);
109109
assertEquals(target, targetBlob.info());
110+
assertEquals(capturedCopyRequest.getValue().sourceBlob(), blob.info().name());
111+
assertEquals(capturedCopyRequest.getValue().sourceBucket(), blob.info().bucket());
112+
assertEquals(capturedCopyRequest.getValue().target(), target);
110113
assertSame(storage, targetBlob.storage());
111114
}
112115

0 commit comments

Comments
 (0)