Skip to content

Commit 3930ff9

Browse files
committed
Merge pull request #688 from aozarov/master
Add a versions option to BlobListOption
2 parents bbe7b50 + e66c9a4 commit 3930ff9

File tree

5 files changed

+71
-17
lines changed

5 files changed

+71
-17
lines changed

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

+9
Original file line numberDiff line numberDiff line change
@@ -700,6 +700,15 @@ public static BlobListOption recursive(boolean recursive) {
700700
return new BlobListOption(StorageRpc.Option.DELIMITER, recursive);
701701
}
702702

703+
/**
704+
* If set to {@code true}, lists all versions of a blob. The default is {@code false}.
705+
*
706+
* @see <a href ="https://cloud.google.com/storage/docs/object-versioning">Object Versioning</a>
707+
*/
708+
public static BlobListOption versions(boolean versions) {
709+
return new BlobListOption(StorageRpc.Option.VERSIONS, versions);
710+
}
711+
703712
/**
704713
* Returns an option to specify the blob's fields to be returned by the RPC call. If this option
705714
* is not provided all blob's fields are returned. {@code BlobListOption.fields}) can be used to

gcloud-java-storage/src/main/java/com/google/gcloud/storage/testing/RemoteGcsHelper.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import com.google.gcloud.RetryParams;
2121
import com.google.gcloud.storage.BlobInfo;
2222
import com.google.gcloud.storage.Storage;
23+
import com.google.gcloud.storage.Storage.BlobListOption;
2324
import com.google.gcloud.storage.StorageException;
2425
import com.google.gcloud.storage.StorageOptions;
2526

@@ -173,8 +174,8 @@ public DeleteBucketTask(Storage storage, String bucket) {
173174
@Override
174175
public Boolean call() {
175176
while (true) {
176-
for (BlobInfo info : storage.list(bucket).values()) {
177-
storage.delete(bucket, info.name());
177+
for (BlobInfo info : storage.list(bucket, BlobListOption.versions(true)).values()) {
178+
storage.delete(info.blobId());
178179
}
179180
try {
180181
storage.delete(bucket);

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

+14-9
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import com.google.common.collect.ImmutableList;
2323
import com.google.gcloud.Page;
24+
import com.google.gcloud.storage.Storage.BlobListOption;
2425
import com.google.gcloud.storage.testing.RemoteGcsHelper;
2526

2627
import org.easymock.EasyMock;
@@ -117,9 +118,10 @@ public Iterator<Blob> iterateAll() {
117118
@Test
118119
public void testForceDelete() throws InterruptedException, ExecutionException {
119120
Storage storageMock = EasyMock.createMock(Storage.class);
120-
EasyMock.expect(storageMock.list(BUCKET_NAME)).andReturn(blobPage);
121+
EasyMock.expect(storageMock.list(BUCKET_NAME, BlobListOption.versions(true)))
122+
.andReturn(blobPage);
121123
for (BlobInfo info : blobList) {
122-
EasyMock.expect(storageMock.delete(BUCKET_NAME, info.name())).andReturn(true);
124+
EasyMock.expect(storageMock.delete(info.blobId())).andReturn(true);
123125
}
124126
EasyMock.expect(storageMock.delete(BUCKET_NAME)).andReturn(true);
125127
EasyMock.replay(storageMock);
@@ -132,7 +134,7 @@ public void testForceDeleteTimeout() throws InterruptedException, ExecutionExcep
132134
Storage storageMock = EasyMock.createMock(Storage.class);
133135
EasyMock.expect(storageMock.list(BUCKET_NAME)).andReturn(blobPage).anyTimes();
134136
for (BlobInfo info : blobList) {
135-
EasyMock.expect(storageMock.delete(BUCKET_NAME, info.name())).andReturn(true).anyTimes();
137+
EasyMock.expect(storageMock.delete(info.blobId())).andReturn(true).anyTimes();
136138
}
137139
EasyMock.expect(storageMock.delete(BUCKET_NAME)).andThrow(RETRYABLE_EXCEPTION).anyTimes();
138140
EasyMock.replay(storageMock);
@@ -143,9 +145,10 @@ public void testForceDeleteTimeout() throws InterruptedException, ExecutionExcep
143145
@Test
144146
public void testForceDeleteFail() throws InterruptedException, ExecutionException {
145147
Storage storageMock = EasyMock.createMock(Storage.class);
146-
EasyMock.expect(storageMock.list(BUCKET_NAME)).andReturn(blobPage);
148+
EasyMock.expect(storageMock.list(BUCKET_NAME, BlobListOption.versions(true)))
149+
.andReturn(blobPage);
147150
for (BlobInfo info : blobList) {
148-
EasyMock.expect(storageMock.delete(BUCKET_NAME, info.name())).andReturn(true);
151+
EasyMock.expect(storageMock.delete(info.blobId())).andReturn(true);
149152
}
150153
EasyMock.expect(storageMock.delete(BUCKET_NAME)).andThrow(FATAL_EXCEPTION);
151154
EasyMock.replay(storageMock);
@@ -160,9 +163,10 @@ public void testForceDeleteFail() throws InterruptedException, ExecutionExceptio
160163
@Test
161164
public void testForceDeleteNoTimeout() {
162165
Storage storageMock = EasyMock.createMock(Storage.class);
163-
EasyMock.expect(storageMock.list(BUCKET_NAME)).andReturn(blobPage);
166+
EasyMock.expect(storageMock.list(BUCKET_NAME, BlobListOption.versions(true)))
167+
.andReturn(blobPage);
164168
for (BlobInfo info : blobList) {
165-
EasyMock.expect(storageMock.delete(BUCKET_NAME, info.name())).andReturn(true);
169+
EasyMock.expect(storageMock.delete(info.blobId())).andReturn(true);
166170
}
167171
EasyMock.expect(storageMock.delete(BUCKET_NAME)).andReturn(true);
168172
EasyMock.replay(storageMock);
@@ -173,9 +177,10 @@ public void testForceDeleteNoTimeout() {
173177
@Test
174178
public void testForceDeleteNoTimeoutFail() {
175179
Storage storageMock = EasyMock.createMock(Storage.class);
176-
EasyMock.expect(storageMock.list(BUCKET_NAME)).andReturn(blobPage);
180+
EasyMock.expect(storageMock.list(BUCKET_NAME, BlobListOption.versions(true)))
181+
.andReturn(blobPage);
177182
for (BlobInfo info : blobList) {
178-
EasyMock.expect(storageMock.delete(BUCKET_NAME, info.name())).andReturn(true);
183+
EasyMock.expect(storageMock.delete(info.blobId())).andReturn(true);
179184
}
180185
EasyMock.expect(storageMock.delete(BUCKET_NAME)).andThrow(FATAL_EXCEPTION);
181186
EasyMock.replay(storageMock);

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

+6-2
Original file line numberDiff line numberDiff line change
@@ -200,11 +200,14 @@ public class StorageImplTest {
200200
Storage.BlobListOption.prefix("prefix");
201201
private static final Storage.BlobListOption BLOB_LIST_FIELDS =
202202
Storage.BlobListOption.fields(Storage.BlobField.CONTENT_TYPE, Storage.BlobField.MD5HASH);
203+
private static final Storage.BlobListOption BLOB_LIST_VERSIONS =
204+
Storage.BlobListOption.versions(false);
203205
private static final Storage.BlobListOption BLOB_LIST_EMPTY_FIELDS =
204206
Storage.BlobListOption.fields();
205207
private static final Map<StorageRpc.Option, ?> BLOB_LIST_OPTIONS = ImmutableMap.of(
206208
StorageRpc.Option.MAX_RESULTS, BLOB_LIST_MAX_RESULT.value(),
207-
StorageRpc.Option.PREFIX, BLOB_LIST_PREFIX.value());
209+
StorageRpc.Option.PREFIX, BLOB_LIST_PREFIX.value(),
210+
StorageRpc.Option.VERSIONS, BLOB_LIST_VERSIONS.value());
208211

209212
private static final String PRIVATE_KEY_STRING = "MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoG"
210213
+ "BAL2xolH1zrISQ8+GzOV29BNjjzq4/HIP8Psd1+cZb81vDklSF+95wB250MSE0BDc81pvIMwj5OmIfLg1NY6uB"
@@ -650,7 +653,8 @@ public void testListBlobsWithOptions() {
650653
EasyMock.replay(storageRpcMock);
651654
initializeService();
652655
ImmutableList<Blob> blobList = ImmutableList.of(expectedBlob1, expectedBlob2);
653-
Page<Blob> page = storage.list(BUCKET_NAME1, BLOB_LIST_MAX_RESULT, BLOB_LIST_PREFIX);
656+
Page<Blob> page =
657+
storage.list(BUCKET_NAME1, BLOB_LIST_MAX_RESULT, BLOB_LIST_PREFIX, BLOB_LIST_VERSIONS);
654658
assertEquals(cursor, page.nextPageCursor());
655659
assertArrayEquals(blobList.toArray(), Iterables.toArray(page.values(), Blob.class));
656660
}

gcloud-java-storage/src/test/java/com/google/gcloud/storage/it/ITStorageTest.java

+39-4
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import com.google.api.client.util.Lists;
2929
import com.google.common.collect.ImmutableList;
3030
import com.google.common.collect.ImmutableMap;
31+
import com.google.common.collect.ImmutableSet;
3132
import com.google.gcloud.Page;
3233
import com.google.gcloud.ReadChannel;
3334
import com.google.gcloud.RestorableState;
@@ -63,6 +64,7 @@
6364
import java.util.List;
6465
import java.util.Map;
6566
import java.util.Random;
67+
import java.util.Set;
6668
import java.util.concurrent.ExecutionException;
6769
import java.util.concurrent.TimeUnit;
6870
import java.util.logging.Level;
@@ -302,8 +304,7 @@ public void testListBlobsSelectedFields() {
302304
Blob remoteBlob2 = storage.create(blob2);
303305
assertNotNull(remoteBlob1);
304306
assertNotNull(remoteBlob2);
305-
Page<Blob> page =
306-
storage.list(BUCKET,
307+
Page<Blob> page = storage.list(BUCKET,
307308
Storage.BlobListOption.prefix("test-list-blobs-selected-fields-blob"),
308309
Storage.BlobListOption.fields(BlobField.METADATA));
309310
int index = 0;
@@ -331,8 +332,7 @@ public void testListBlobsEmptySelectedFields() {
331332
Blob remoteBlob2 = storage.create(blob2);
332333
assertNotNull(remoteBlob1);
333334
assertNotNull(remoteBlob2);
334-
Page<Blob> page = storage.list(
335-
BUCKET,
335+
Page<Blob> page = storage.list(BUCKET,
336336
Storage.BlobListOption.prefix("test-list-blobs-empty-selected-fields-blob"),
337337
Storage.BlobListOption.fields());
338338
int index = 0;
@@ -345,6 +345,41 @@ public void testListBlobsEmptySelectedFields() {
345345
assertTrue(remoteBlob2.delete());
346346
}
347347

348+
@Test
349+
public void testListBlobsVersioned() throws ExecutionException, InterruptedException {
350+
String bucketName = RemoteGcsHelper.generateBucketName();
351+
Bucket bucket = storage.create(BucketInfo.builder(bucketName).versioningEnabled(true).build());
352+
try {
353+
String[] blobNames = {"test-list-blobs-versioned-blob1", "test-list-blobs-versioned-blob2"};
354+
BlobInfo blob1 = BlobInfo.builder(bucket, blobNames[0])
355+
.contentType(CONTENT_TYPE)
356+
.build();
357+
BlobInfo blob2 = BlobInfo.builder(bucket, blobNames[1])
358+
.contentType(CONTENT_TYPE)
359+
.build();
360+
Blob remoteBlob1 = storage.create(blob1);
361+
Blob remoteBlob2 = storage.create(blob2);
362+
Blob remoteBlob3 = storage.create(blob2);
363+
assertNotNull(remoteBlob1);
364+
assertNotNull(remoteBlob2);
365+
assertNotNull(remoteBlob3);
366+
Page<Blob> page = storage.list(bucketName,
367+
Storage.BlobListOption.prefix("test-list-blobs-versioned-blob"),
368+
Storage.BlobListOption.versions(true));
369+
Set<String> blobSet = ImmutableSet.of(blobNames[0], blobNames[1]);
370+
for (Blob remoteBlob : page.values()) {
371+
assertEquals(bucketName, remoteBlob.bucket());
372+
assertTrue(blobSet.contains(remoteBlob.name()));
373+
assertNotNull(remoteBlob.generation());
374+
}
375+
assertTrue(remoteBlob1.delete());
376+
assertTrue(remoteBlob2.delete());
377+
assertTrue(remoteBlob3.delete());
378+
} finally {
379+
RemoteGcsHelper.forceDelete(storage, bucketName, 5, TimeUnit.SECONDS);
380+
}
381+
}
382+
348383
@Test
349384
public void testUpdateBlob() {
350385
String blobName = "test-update-blob";

0 commit comments

Comments
 (0)