Skip to content

Commit f030701

Browse files
jean-philippe-martinchingor13
authored andcommitted
Add a feature to list the buckets (#3915)
Also, update the README to reflect recent update to pseudoDirs.
1 parent f79ff73 commit f030701

4 files changed

Lines changed: 76 additions & 5 deletions

File tree

java-storage-nio/google-cloud-nio/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,6 @@ Limitations
138138

139139
This library is usable, but not yet complete. The following features are not
140140
yet implemented:
141-
* Listing all the buckets
142141
* Resuming upload or download
143142
* Generations
144143
* File attributes
@@ -153,8 +152,9 @@ subset via a familiar interface.
153152
**NOTE:** Cloud Storage uses a flat namespace and therefore doesn't support real
154153
directories. So this library supports what's known as "pseudo-directories". Any
155154
path that includes a trailing slash, will be considered a directory. It will
156-
always be assumed to exist, without performing any I/O. This allows you to do
157-
path manipulation in the same manner as you would with the normal UNIX file
155+
always be assumed to exist, without performing any I/O. Paths without the trailing
156+
slash will result in an I/O operation to check a file is present in that "directory".
157+
This allows you to do path manipulation in the same manner as you would with the normal UNIX file
158158
system implementation. You can disable this feature with
159159
`CloudStorageConfiguration.usePseudoDirectories()`.
160160

java-storage-nio/google-cloud-nio/src/main/java/com/google/cloud/storage/contrib/nio/CloudStorageFileSystem.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@
1919
import static com.google.common.base.Preconditions.checkArgument;
2020
import static com.google.common.base.Preconditions.checkNotNull;
2121

22+
import com.google.api.gax.paging.Page;
23+
import com.google.cloud.storage.Bucket;
24+
import com.google.cloud.storage.Storage;
25+
import com.google.cloud.storage.StorageException;
2226
import com.google.cloud.storage.StorageOptions;
2327
import com.google.common.base.Strings;
2428
import com.google.common.collect.ImmutableSet;
@@ -85,6 +89,27 @@ static CloudStorageConfiguration getDefaultCloudStorageConfiguration() {
8589
return userSpecifiedDefault;
8690
}
8791

92+
/**
93+
* Lists the project's buckets. Pass "null" to use the default project.
94+
*
95+
* <p>Example of listing buckets, specifying the page size and a name prefix.
96+
* <pre> {@code
97+
* String prefix = "bucket_";
98+
* Page<Bucket> buckets = CloudStorageFileSystem.listBuckets("my-project", BucketListOption.prefix(prefix));
99+
* Iterator<Bucket> bucketIterator = buckets.iterateAll();
100+
* while (bucketIterator.hasNext()) {
101+
* Bucket bucket = bucketIterator.next();
102+
* // do something with the bucket
103+
* }
104+
* }</pre>
105+
*
106+
* @throws StorageException upon failure
107+
*/
108+
public static Page<Bucket> listBuckets(@Nullable String project, Storage.BucketListOption... options) {
109+
CloudStorageFileSystemProvider provider = new CloudStorageFileSystemProvider(null, StorageOptions.newBuilder().setProjectId(project).build());
110+
return provider.listBuckets(options);
111+
}
112+
88113
/**
89114
* Returns Google Cloud Storage {@link FileSystem} object for {@code bucket}.
90115
*

java-storage-nio/google-cloud-nio/src/main/java/com/google/cloud/storage/contrib/nio/CloudStorageFileSystemProvider.java

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import com.google.cloud.storage.Blob;
2727
import com.google.cloud.storage.BlobId;
2828
import com.google.cloud.storage.BlobInfo;
29+
import com.google.cloud.storage.Bucket;
2930
import com.google.cloud.storage.CopyWriter;
3031
import com.google.cloud.storage.Storage;
3132
import com.google.cloud.storage.Storage.BlobGetOption;
@@ -34,7 +35,6 @@
3435
import com.google.cloud.storage.StorageOptions;
3536
import com.google.common.annotations.VisibleForTesting;
3637
import com.google.common.base.MoreObjects;
37-
import com.google.common.base.Strings;
3838
import com.google.common.collect.AbstractIterator;
3939
import com.google.common.net.UrlEscapers;
4040
import com.google.common.primitives.Ints;
@@ -915,6 +915,35 @@ public CloudStorageFileSystemProvider withNoUserProject() {
915915
return new CloudStorageFileSystemProvider("", this.storageOptions);
916916
}
917917

918+
/**
919+
* Returns the project that is assigned to this provider.
920+
*/
921+
public String getProject() {
922+
initStorage();
923+
return storage.getOptions().getProjectId();
924+
}
925+
926+
/**
927+
* Lists the project's buckets. But use the one in CloudStorageFileSystem.
928+
*
929+
* <p>Example of listing buckets, specifying the page size and a name prefix.
930+
* <pre> {@code
931+
* String prefix = "bucket_";
932+
* Page<Bucket> buckets = provider.listBuckets(BucketListOption.prefix(prefix));
933+
* Iterator<Bucket> bucketIterator = buckets.iterateAll();
934+
* while (bucketIterator.hasNext()) {
935+
* Bucket bucket = bucketIterator.next();
936+
* // do something with the bucket
937+
* }
938+
* }</pre>
939+
*
940+
* @throws StorageException upon failure
941+
*/
942+
Page<Bucket> listBuckets(Storage.BucketListOption... options) {
943+
initStorage();
944+
return storage.list(options);
945+
}
946+
918947
private IOException asIoException(StorageException oops) {
919948
// RPC API can only throw StorageException, but CloudStorageFileSystemProvider
920949
// can only throw IOException. Square peg, round hole.

java-storage-nio/google-cloud-nio/src/test/java/com/google/cloud/storage/contrib/nio/it/ITGcsNio.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,13 @@
1616

1717
package com.google.cloud.storage.contrib.nio.it;
1818

19+
import static com.google.common.collect.ImmutableList.copyOf;
1920
import static com.google.common.truth.Truth.assertThat;
2021
import static com.google.common.truth.Truth.assertWithMessage;
2122
import static java.nio.charset.StandardCharsets.UTF_8;
2223

2324
import com.google.api.client.http.HttpResponseException;
25+
import com.google.cloud.storage.Bucket;
2426
import com.google.cloud.storage.Storage.BlobTargetOption;
2527
import com.google.cloud.storage.StorageException;
2628
import com.google.cloud.storage.contrib.nio.CloudStorageConfiguration;
@@ -298,6 +300,21 @@ private void assertIsRequesterPaysException(String message, StorageException sex
298300

299301
// End of tests related to the "requester pays" feature
300302

303+
@Test
304+
public void testListBuckets() throws IOException {
305+
boolean bucketFound = false;
306+
boolean rpBucketFound = false;
307+
for (Bucket b : CloudStorageFileSystem.listBuckets(project).iterateAll()) {
308+
bucketFound |= BUCKET.equals(b.getName());
309+
rpBucketFound |= REQUESTER_PAYS_BUCKET.equals(b.getName());
310+
}
311+
assertWithMessage("listBucket should have found the test bucket")
312+
.that(bucketFound).isTrue();
313+
assertWithMessage("listBucket should have found the test requester-pays bucket")
314+
.that(rpBucketFound).isTrue();
315+
}
316+
317+
301318
@Test
302319
public void testFileExists() throws IOException {
303320
CloudStorageFileSystem testBucket = getTestBucket();
@@ -736,7 +753,7 @@ public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOEx
736753
}
737754

738755
public ImmutableList<Path> getPaths() {
739-
return ImmutableList.copyOf(paths);
756+
return copyOf(paths);
740757
}
741758
}
742759

0 commit comments

Comments
 (0)