File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -1003,6 +1003,16 @@ public static BlobListOption currentDirectory() {
10031003
return new BlobListOption(StorageRpc.Option.DELIMITER, true);
10041004
}
10051005

1006+
/**
1007+
* Returns an option to set a delimiter.
1008+
*
1009+
* @param delimiter generally '/' is the one used most often, but you can used other delimiters
1010+
* as well.
1011+
*/
1012+
public static BlobListOption delimiter(String delimiter) {
1013+
return new BlobListOption(StorageRpc.Option.DELIMITER, delimiter);
1014+
}
1015+
10061016
/**
10071017
* Returns an option to define the billing user project. This option is required by buckets with
10081018
* `requester_pays` flag enabled to assign operation costs.
Original file line numberDiff line numberDiff line change
@@ -1594,9 +1594,11 @@ private static <T> void addToOptionMap(
15941594
Object prev = temp.put(option.getRpcOption(), option.getValue());
15951595
checkArgument(prev == null, "Duplicate option %s", option);
15961596
}
1597-
Boolean value = (Boolean) temp.remove(DELIMITER);
1598-
if (Boolean.TRUE.equals(value)) {
1597+
if (Boolean.TRUE.equals(temp.get(DELIMITER))) {
1598+
temp.remove(DELIMITER);
15991599
temp.put(DELIMITER, PATH_DELIMITER);
1600+
} else if (null != temp.get(DELIMITER)) {
1601+
temp.put(DELIMITER, temp.get(DELIMITER));
16001602
}
16011603
if (useAsSource) {
16021604
addToOptionMap(IF_GENERATION_MATCH, IF_SOURCE_GENERATION_MATCH, generation, temp);
Original file line numberDiff line numberDiff line change
@@ -1166,6 +1166,23 @@ public void testListBlobsCurrentDirectory() {
11661166
assertArrayEquals(blobList.toArray(), Iterables.toArray(page.getValues(), Blob.class));
11671167
}
11681168

1169+
@Test
1170+
public void testListBlobsDelimiter() {
1171+
String cursor = "cursor";
1172+
String delimiter = "/";
1173+
Map<StorageRpc.Option, ?> options = ImmutableMap.of(StorageRpc.Option.DELIMITER, delimiter);
1174+
ImmutableList<BlobInfo> blobInfoList = ImmutableList.of(BLOB_INFO1, BLOB_INFO2);
1175+
Tuple<String, Iterable<com.google.api.services.storage.model.StorageObject>> result =
1176+
Tuple.of(cursor, Iterables.transform(blobInfoList, BlobInfo.INFO_TO_PB_FUNCTION));
1177+
EasyMock.expect(storageRpcMock.list(BUCKET_NAME1, options)).andReturn(result);
1178+
EasyMock.replay(storageRpcMock);
1179+
initializeService();
1180+
ImmutableList<Blob> blobList = ImmutableList.of(expectedBlob1, expectedBlob2);
1181+
Page<Blob> page = storage.list(BUCKET_NAME1, Storage.BlobListOption.delimiter(delimiter));
1182+
assertEquals(cursor, page.getNextPageToken());
1183+
assertArrayEquals(blobList.toArray(), Iterables.toArray(page.getValues(), Blob.class));
1184+
}
1185+
11691186
@Test
11701187
public void testUpdateBucket() {
11711188
BucketInfo updatedBucketInfo = BUCKET_INFO1.toBuilder().setIndexPage("some-page").build();

0 commit comments

Comments
 (0)