diff --git a/gcloud-java-examples/src/main/java/com/google/cloud/examples/storage/snippets/BucketSnippets.java b/gcloud-java-examples/src/main/java/com/google/cloud/examples/storage/snippets/BucketSnippets.java index df6c0d68d742..f9ef51941f26 100644 --- a/gcloud-java-examples/src/main/java/com/google/cloud/examples/storage/snippets/BucketSnippets.java +++ b/gcloud-java-examples/src/main/java/com/google/cloud/examples/storage/snippets/BucketSnippets.java @@ -24,6 +24,7 @@ import static java.nio.charset.StandardCharsets.UTF_8; +import com.google.cloud.Page; import com.google.cloud.storage.Blob; import com.google.cloud.storage.Bucket; import com.google.cloud.storage.Bucket.BucketSourceOption; @@ -110,15 +111,16 @@ public boolean delete() { * Example of listing the blobs in the bucket. */ // [TARGET list(BlobListOption...)] - public Iterator listBlobs() { + public Page listBlobs() { // [START listBlobs] - Iterator blobIterator = bucket.list().iterateAll(); + Page blobs = bucket.list(); + Iterator blobIterator = blobs.iterateAll(); while (blobIterator.hasNext()) { Blob blob = blobIterator.next(); // do something with the blob } // [END listBlobs] - return blobIterator; + return blobs; } /** diff --git a/gcloud-java-examples/src/main/java/com/google/cloud/examples/storage/snippets/StorageSnippets.java b/gcloud-java-examples/src/main/java/com/google/cloud/examples/storage/snippets/StorageSnippets.java index f29bde49d558..88f39f9c98ed 100644 --- a/gcloud-java-examples/src/main/java/com/google/cloud/examples/storage/snippets/StorageSnippets.java +++ b/gcloud-java-examples/src/main/java/com/google/cloud/examples/storage/snippets/StorageSnippets.java @@ -201,7 +201,7 @@ public Blob getBlobFromIdWithMetageneration(String bucketName, String blobName, */ // [TARGET list(BucketListOption...)] // [VARIABLE "bucket_"] - public Iterator listBucketsWithSizeAndPrefix(String prefix) { + public Page listBucketsWithSizeAndPrefix(String prefix) { // [START listBucketsWithSizeAndPrefix] Page buckets = storage.list(BucketListOption.pageSize(100), BucketListOption.prefix(prefix)); @@ -211,16 +211,16 @@ public Iterator listBucketsWithSizeAndPrefix(String prefix) { // do something with the bucket } // [END listBucketsWithSizeAndPrefix] - return bucketIterator; + return buckets; } /** - * Example of listing buckets, specifying the page size and a name prefix. + * Example of listing blobs in a provided directory. */ // [TARGET list(String, BlobListOption...)] // [VARIABLE "my_unique_bucket"] - // [VARIABLE "my_directory"] - public Iterator listBlobsWithDirectoryAndPrefix(String bucketName, String directory) { + // [VARIABLE "my_directory/"] + public Page listBlobsWithDirectoryAndPrefix(String bucketName, String directory) { // [START listBlobsWithDirectoryAndPrefix] Page blobs = storage.list(bucketName, BlobListOption.currentDirectory(), BlobListOption.prefix(directory)); @@ -230,7 +230,7 @@ public Iterator listBlobsWithDirectoryAndPrefix(String bucketName, String // do something with the blob } // [END listBlobsWithDirectoryAndPrefix] - return blobIterator; + return blobs; } /** diff --git a/gcloud-java-examples/src/test/java/com/google/cloud/example/storage/snippets/ITBlobSnippets.java b/gcloud-java-examples/src/test/java/com/google/cloud/examples/storage/snippets/ITBlobSnippets.java similarity index 97% rename from gcloud-java-examples/src/test/java/com/google/cloud/example/storage/snippets/ITBlobSnippets.java rename to gcloud-java-examples/src/test/java/com/google/cloud/examples/storage/snippets/ITBlobSnippets.java index 39c3e5d5b6db..5fe46a54b722 100644 --- a/gcloud-java-examples/src/test/java/com/google/cloud/example/storage/snippets/ITBlobSnippets.java +++ b/gcloud-java-examples/src/test/java/com/google/cloud/examples/storage/snippets/ITBlobSnippets.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package com.google.cloud.example.storage.snippets; +package com.google.cloud.examples.storage.snippets; import static java.nio.charset.StandardCharsets.UTF_8; import static org.junit.Assert.assertArrayEquals; @@ -24,7 +24,6 @@ import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; -import com.google.cloud.examples.storage.snippets.BlobSnippets; import com.google.cloud.storage.Blob; import com.google.cloud.storage.BlobId; import com.google.cloud.storage.BlobInfo; diff --git a/gcloud-java-examples/src/test/java/com/google/cloud/examples/storage/snippets/ITBucketSnippets.java b/gcloud-java-examples/src/test/java/com/google/cloud/examples/storage/snippets/ITBucketSnippets.java index 95549af2b732..5a8dd68f77a4 100644 --- a/gcloud-java-examples/src/test/java/com/google/cloud/examples/storage/snippets/ITBucketSnippets.java +++ b/gcloud-java-examples/src/test/java/com/google/cloud/examples/storage/snippets/ITBucketSnippets.java @@ -26,15 +26,15 @@ import com.google.cloud.storage.Storage; import com.google.cloud.storage.StorageException; import com.google.cloud.storage.testing.RemoteStorageHelper; -import com.google.common.collect.ImmutableSet; +import com.google.common.collect.Sets; import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; +import org.junit.rules.Timeout; -import java.util.Iterator; import java.util.List; import java.util.Set; import java.util.concurrent.ExecutionException; @@ -50,7 +50,6 @@ public class ITBucketSnippets { private static final String BLOB2 = "blob2"; private static final String BLOB3 = "blob3"; private static final String BLOB4 = "blob4"; - private static final Set BLOBS = ImmutableSet.of(BLOB1, BLOB2, BLOB3, BLOB4); private static Storage storage; private static BucketSnippets bucketSnippets; @@ -58,6 +57,9 @@ public class ITBucketSnippets { @Rule public ExpectedException thrown = ExpectedException.none(); + @Rule + public Timeout globalTimeout = Timeout.seconds(300); + @BeforeClass public static void beforeClass() { RemoteStorageHelper helper = RemoteStorageHelper.create(); @@ -76,7 +78,7 @@ public static void afterClass() throws ExecutionException, InterruptedException } @Test - public void testBucket() { + public void testBucket() throws InterruptedException { assertTrue(bucketSnippets.exists()); Bucket bucket = bucketSnippets.reload(); assertNotNull(bucket); @@ -90,10 +92,15 @@ public void testBucket() { assertNotNull(blob3); Blob blob4 = bucketSnippets.createBlobFromInputStreamWithContentType(BLOB4); assertNotNull(blob4); - Iterator blobIterator = bucketSnippets.listBlobs(); - while (blobIterator.hasNext()) { - assertTrue(BLOBS.contains(blobIterator.next().name())); + Set blobSet = Sets.newHashSet(bucketSnippets.listBlobs().iterateAll()); + while (blobSet.size() < 4) { + Thread.sleep(500); + blobSet = Sets.newHashSet(bucketSnippets.listBlobs().iterateAll()); } + assertTrue(blobSet.contains(blob1)); + assertTrue(blobSet.contains(blob2)); + assertTrue(blobSet.contains(blob3)); + assertTrue(blobSet.contains(blob4)); blob1 = bucketSnippets.getBlob(BLOB1, blob1.generation()); assertEquals(BLOB1, blob1.name()); List blobs = bucketSnippets.getBlobFromStrings(BLOB2, BLOB3); diff --git a/gcloud-java-examples/src/test/java/com/google/cloud/examples/storage/snippets/ITStorageSnippets.java b/gcloud-java-examples/src/test/java/com/google/cloud/examples/storage/snippets/ITStorageSnippets.java index 2e930df6564e..363bfe80f464 100644 --- a/gcloud-java-examples/src/test/java/com/google/cloud/examples/storage/snippets/ITStorageSnippets.java +++ b/gcloud-java-examples/src/test/java/com/google/cloud/examples/storage/snippets/ITStorageSnippets.java @@ -24,25 +24,30 @@ import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; +import com.google.cloud.Page; import com.google.cloud.storage.Blob; import com.google.cloud.storage.BlobInfo; import com.google.cloud.storage.Bucket; import com.google.cloud.storage.Storage; import com.google.cloud.storage.StorageException; import com.google.cloud.storage.testing.RemoteStorageHelper; +import com.google.common.collect.Iterators; import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; +import org.junit.rules.Timeout; import java.io.IOException; import java.io.InputStream; import java.net.URL; import java.net.URLConnection; +import java.util.HashSet; import java.util.Iterator; import java.util.List; +import java.util.Set; import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit; import java.util.logging.Level; @@ -59,6 +64,9 @@ public class ITStorageSnippets { @Rule public ExpectedException thrown = ExpectedException.none(); + @Rule + public Timeout globalTimeout = Timeout.seconds(300); + @BeforeClass public static void beforeClass() { RemoteStorageHelper helper = RemoteStorageHelper.create(); @@ -78,7 +86,7 @@ public static void afterClass() throws ExecutionException, InterruptedException } @Test - public void testBlob() { + public void testBlob() throws InterruptedException { String blobName = "directory/test-blob"; Blob blob = storageSnippets.createBlob(BUCKET, blobName); assertNotNull(blob); @@ -88,12 +96,20 @@ public void testBlob() { assertNotNull(blob); blob = storageSnippets.updateBlobWithMetageneration(BUCKET, blobName); assertNotNull(blob); - blob = storageSnippets.copyBlob(BUCKET, blobName, "directory/copy-blob"); - assertNotNull(blob); - Iterator blobs = storageSnippets.listBlobsWithDirectoryAndPrefix(BUCKET, "directory"); - while (blobs.hasNext()) { - assertTrue(blobs.next().name().startsWith("directory")); - } blob.delete(); + Blob copiedBlob = storageSnippets.copyBlob(BUCKET, blobName, "directory/copy-blob"); + assertNotNull(copiedBlob); + Page blobs = storageSnippets.listBlobsWithDirectoryAndPrefix(BUCKET, "directory/"); + while (Iterators.size(blobs.iterateAll()) < 2) { + Thread.sleep(500); + blobs = storageSnippets.listBlobsWithDirectoryAndPrefix(BUCKET, "directory/"); + } + Set blobNames = new HashSet<>(); + Iterator blobIterator = blobs.iterateAll(); + while (blobIterator.hasNext()) { + blobNames.add(blobIterator.next().name()); + } + assertTrue(blobNames.contains(blobName)); + assertTrue(blobNames.contains("directory/copy-blob")); try { storageSnippets.getBlobFromStringsWithMetageneration(BUCKET, blobName, -1); fail("Expected StorageException to be thrown"); @@ -101,6 +117,7 @@ public void testBlob() { // expected } assertTrue(storageSnippets.deleteBlob(BUCKET, blobName)); + copiedBlob.delete(); } @Test @@ -136,10 +153,15 @@ public void testGetBucketWithMetageneration() { } @Test - public void testListBucketsWithSizeAndPrefix() { - Iterator buckets = storageSnippets.listBucketsWithSizeAndPrefix(BUCKET); - while (buckets.hasNext()) { - assertTrue(buckets.next().name().startsWith(BUCKET)); + public void testListBucketsWithSizeAndPrefix() throws InterruptedException { + Page buckets = storageSnippets.listBucketsWithSizeAndPrefix(BUCKET); + while (Iterators.size(buckets.iterateAll()) < 1) { + Thread.sleep(500); + buckets = storageSnippets.listBucketsWithSizeAndPrefix(BUCKET); + } + Iterator bucketIterator = buckets.iterateAll(); + while (bucketIterator.hasNext()) { + assertTrue(bucketIterator.next().name().startsWith(BUCKET)); } } diff --git a/gcloud-java-storage/src/main/java/com/google/cloud/storage/Bucket.java b/gcloud-java-storage/src/main/java/com/google/cloud/storage/Bucket.java index d342eb92146c..4450f9689e72 100644 --- a/gcloud-java-storage/src/main/java/com/google/cloud/storage/Bucket.java +++ b/gcloud-java-storage/src/main/java/com/google/cloud/storage/Bucket.java @@ -618,7 +618,8 @@ public boolean delete(BucketSourceOption... options) { * *

Example of listing the blobs in the bucket. *

 {@code
-   * Iterator blobIterator = bucket.list().iterateAll();
+   * Page blobs = bucket.list();
+   * Iterator blobIterator = blobs.iterateAll();
    * while (blobIterator.hasNext()) {
    *   Blob blob = blobIterator.next();
    *   // do something with the blob
diff --git a/gcloud-java-storage/src/main/java/com/google/cloud/storage/Storage.java b/gcloud-java-storage/src/main/java/com/google/cloud/storage/Storage.java
index ae98e6b46e4c..1e9dc392ce31 100644
--- a/gcloud-java-storage/src/main/java/com/google/cloud/storage/Storage.java
+++ b/gcloud-java-storage/src/main/java/com/google/cloud/storage/Storage.java
@@ -1371,10 +1371,10 @@ public static Builder builder() {
    * Lists the bucket's blobs. If the {@link BlobListOption#currentDirectory()} option is provided,
    * results are returned in a directory-like mode.
    *
-   * 

Example of listing buckets, specifying the page size and a name prefix. + *

Example of listing blobs in a provided directory. *

 {@code
    * String bucketName = "my_unique_bucket";
-   * String directory = "my_directory";
+   * String directory = "my_directory/";
    * Page blobs = storage.list(bucketName, BlobListOption.currentDirectory(),
    *     BlobListOption.prefix(directory));
    * Iterator blobIterator = blobs.iterateAll();