/** * Lists the project's buckets. But use the one in CloudStorageFileSystem. * * <p>Example of listing buckets, specifying the page size and a name prefix. * * <pre>{@code * String prefix = "bucket_"; * Page<Bucket> buckets = provider.listBuckets(BucketListOption.prefix(prefix)); * Iterator<Bucket> bucketIterator = buckets.iterateAll(); * while (bucketIterator.hasNext()) { * Bucket bucket = bucketIterator.next(); * // do something with the bucket * } * }</pre> * * @throws StorageException upon failure */ Page<Bucket> listBuckets(Storage.BucketListOption... options) { initStorage(); return storage.list(options); }
/** * Returns the paginated list of {@code Blob} in this bucket. * * <p>Example of listing the blobs in the bucket. * * <pre>{@code * Page<Blob> blobs = bucket.list(); * Iterator<Blob> blobIterator = blobs.iterateAll(); * while (blobIterator.hasNext()) { * Blob blob = blobIterator.next(); * // do something with the blob * } * }</pre> * * @param options options for listing blobs * @throws StorageException upon failure */ public Page<Blob> list(BlobListOption... options) { return storage.list(getName(), options); }
@Override public void run(Storage storage, String bucketName) { if (bucketName == null) { // list buckets for (Bucket bucket : storage.list().iterateAll()) { System.out.println(bucket); } } else { // list a bucket's blobs Bucket bucket = storage.get(bucketName); if (bucket == null) { System.out.println("No such bucket"); return; } for (Blob blob : bucket.list().iterateAll()) { System.out.println(blob); } } }
/** Example of default auth */ public Page<Bucket> authListBuckets() { // [START auth_cloud_implicit] // If you don't specify credentials when constructing the client, the // client library will look for credentials in the environment. Storage storage = StorageOptions.getDefaultInstance().getService(); Page<Bucket> buckets = storage.list(); for (Bucket bucket : buckets.iterateAll()) { // do something with the info } // [END auth_cloud_implicit] return buckets; }
/** Example of listing buckets, specifying the page size and a name prefix. */ // [TARGET list(BucketListOption...)] // [VARIABLE "bucket_"] public Page<Bucket> listBucketsWithSizeAndPrefix(String prefix) { // [START listBucketsWithSizeAndPrefix] // Include a prefix of bucket-name to reduce search space. // For more information read https://cloud.google.com/storage/docs/json_api/v1/buckets/list Page<Bucket> buckets = storage.list(BucketListOption.pageSize(100), BucketListOption.prefix(prefix)); for (Bucket bucket : buckets.iterateAll()) { // do something with the bucket } // [END listBucketsWithSizeAndPrefix] return buckets; }
/** Example of listing blobs in a provided directory. */ // [TARGET list(String, BlobListOption...)] // [VARIABLE "my_unique_bucket"] // [VARIABLE "my_directory/"] public Page<Blob> listBlobsWithDirectoryAndPrefix(String bucketName, String directory) { // [START listBlobsWithDirectoryAndPrefix] Page<Blob> blobs = storage.list( bucketName, BlobListOption.currentDirectory(), BlobListOption.prefix(directory)); for (Blob blob : blobs.iterateAll()) { // do something with the blob } // [END listBlobsWithDirectoryAndPrefix] return blobs; }
@Test public void testForceDelete() throws InterruptedException, ExecutionException { Storage storageMock = EasyMock.createMock(Storage.class); EasyMock.expect(blob1.getBlobId()).andReturn(BLOB_ID1); EasyMock.expect(blob2.getBlobId()).andReturn(BLOB_ID2); ArrayList<BlobId> ids = new ArrayList<>(); ids.add(BLOB_ID1); ids.add(BLOB_ID2); EasyMock.expect(storageMock.delete(ids)).andReturn(Collections.nCopies(2, true)); EasyMock.expect(storageMock.list(BUCKET_NAME, BlobListOption.versions(true))) .andReturn(blobPage); EasyMock.expect(storageMock.delete(BUCKET_NAME)).andReturn(true); EasyMock.replay(storageMock, blob1, blob2); assertTrue(RemoteStorageHelper.forceDelete(storageMock, BUCKET_NAME, 5, TimeUnit.SECONDS)); EasyMock.verify(storageMock, blob1, blob2); }
@Test public void testForceDeleteNoTimeout() { Storage storageMock = EasyMock.createMock(Storage.class); EasyMock.expect(blob1.getBlobId()).andReturn(BLOB_ID1); EasyMock.expect(blob2.getBlobId()).andReturn(BLOB_ID2); ArrayList<BlobId> ids = new ArrayList<>(); ids.add(BLOB_ID1); ids.add(BLOB_ID2); EasyMock.expect(storageMock.delete(ids)).andReturn(Collections.nCopies(2, true)).anyTimes(); EasyMock.expect(storageMock.list(BUCKET_NAME, BlobListOption.versions(true))) .andReturn(blobPage); EasyMock.expect(storageMock.delete(BUCKET_NAME)).andReturn(true); EasyMock.replay(storageMock, blob1, blob2); RemoteStorageHelper.forceDelete(storageMock, BUCKET_NAME); EasyMock.verify(storageMock); }
@Test public void testForceDeleteFail() throws InterruptedException, ExecutionException { Storage storageMock = EasyMock.createMock(Storage.class); EasyMock.expect(blob1.getBlobId()).andReturn(BLOB_ID1); EasyMock.expect(blob2.getBlobId()).andReturn(BLOB_ID2); ArrayList<BlobId> ids = new ArrayList<>(); ids.add(BLOB_ID1); ids.add(BLOB_ID2); EasyMock.expect(storageMock.delete(ids)).andReturn(Collections.nCopies(2, true)).anyTimes(); EasyMock.expect(storageMock.list(BUCKET_NAME, BlobListOption.versions(true))) .andReturn(blobPage); EasyMock.expect(storageMock.delete(BUCKET_NAME)).andThrow(FATAL_EXCEPTION); EasyMock.replay(storageMock, blob1, blob2); thrown.expect(ExecutionException.class); try { RemoteStorageHelper.forceDelete(storageMock, BUCKET_NAME, 5, TimeUnit.SECONDS); } finally { EasyMock.verify(storageMock); } }
@Test public void testForceDeleteNoTimeoutFail() { Storage storageMock = EasyMock.createMock(Storage.class); EasyMock.expect(blob1.getBlobId()).andReturn(BLOB_ID1); EasyMock.expect(blob2.getBlobId()).andReturn(BLOB_ID2); ArrayList<BlobId> ids = new ArrayList<>(); ids.add(BLOB_ID1); ids.add(BLOB_ID2); EasyMock.expect(storageMock.delete(ids)).andReturn(Collections.nCopies(2, true)).anyTimes(); EasyMock.expect(storageMock.list(BUCKET_NAME, BlobListOption.versions(true))) .andReturn(blobPage); EasyMock.expect(storageMock.delete(BUCKET_NAME)).andThrow(FATAL_EXCEPTION); EasyMock.replay(storageMock, blob1, blob2); thrown.expect(StorageException.class); try { RemoteStorageHelper.forceDelete(storageMock, BUCKET_NAME); } finally { EasyMock.verify(storageMock); } }
@Test public void testListBlobs() { String cursor = "cursor"; ImmutableList<BlobInfo> blobInfoList = ImmutableList.of(BLOB_INFO1, BLOB_INFO2); Tuple<String, Iterable<com.google.api.services.storage.model.StorageObject>> result = Tuple.of(cursor, Iterables.transform(blobInfoList, BlobInfo.INFO_TO_PB_FUNCTION)); EasyMock.expect(storageRpcMock.list(BUCKET_NAME1, EMPTY_RPC_OPTIONS)).andReturn(result); EasyMock.replay(storageRpcMock); initializeService(); ImmutableList<Blob> blobList = ImmutableList.of(expectedBlob1, expectedBlob2); Page<Blob> page = storage.list(BUCKET_NAME1); assertEquals(cursor, page.getNextPageToken()); assertArrayEquals(blobList.toArray(), Iterables.toArray(page.getValues(), Blob.class)); }
@Test public void testListBlobsCurrentDirectory() { String cursor = "cursor"; Map<StorageRpc.Option, ?> options = ImmutableMap.of(StorageRpc.Option.DELIMITER, "/"); ImmutableList<BlobInfo> blobInfoList = ImmutableList.of(BLOB_INFO1, BLOB_INFO2); Tuple<String, Iterable<com.google.api.services.storage.model.StorageObject>> result = Tuple.of(cursor, Iterables.transform(blobInfoList, BlobInfo.INFO_TO_PB_FUNCTION)); EasyMock.expect(storageRpcMock.list(BUCKET_NAME1, options)).andReturn(result); EasyMock.replay(storageRpcMock); initializeService(); ImmutableList<Blob> blobList = ImmutableList.of(expectedBlob1, expectedBlob2); Page<Blob> page = storage.list(BUCKET_NAME1, Storage.BlobListOption.currentDirectory()); assertEquals(cursor, page.getNextPageToken()); assertArrayEquals(blobList.toArray(), Iterables.toArray(page.getValues(), Blob.class)); }
@Test public void testListBuckets() { String cursor = "cursor"; ImmutableList<BucketInfo> bucketInfoList = ImmutableList.of(BUCKET_INFO1, BUCKET_INFO2); Tuple<String, Iterable<com.google.api.services.storage.model.Bucket>> result = Tuple.of(cursor, Iterables.transform(bucketInfoList, BucketInfo.TO_PB_FUNCTION)); EasyMock.expect(storageRpcMock.list(EMPTY_RPC_OPTIONS)).andReturn(result); EasyMock.replay(storageRpcMock); initializeService(); ImmutableList<Bucket> bucketList = ImmutableList.of(expectedBucket1, expectedBucket2); Page<Bucket> page = storage.list(); assertEquals(cursor, page.getNextPageToken()); assertArrayEquals(bucketList.toArray(), Iterables.toArray(page.getValues(), Bucket.class)); }
@Test public void testListBucketsWithOptions() { String cursor = "cursor"; ImmutableList<BucketInfo> bucketInfoList = ImmutableList.of(BUCKET_INFO1, BUCKET_INFO2); Tuple<String, Iterable<com.google.api.services.storage.model.Bucket>> result = Tuple.of(cursor, Iterables.transform(bucketInfoList, BucketInfo.TO_PB_FUNCTION)); EasyMock.expect(storageRpcMock.list(BUCKET_LIST_OPTIONS)).andReturn(result); EasyMock.replay(storageRpcMock); initializeService(); ImmutableList<Bucket> bucketList = ImmutableList.of(expectedBucket1, expectedBucket2); Page<Bucket> page = storage.list(BUCKET_LIST_PAGE_SIZE, BUCKET_LIST_PREFIX); assertEquals(cursor, page.getNextPageToken()); assertArrayEquals(bucketList.toArray(), Iterables.toArray(page.getValues(), Bucket.class)); }
@Test public void testForceDeleteTimeout() throws InterruptedException, ExecutionException { Storage storageMock = EasyMock.createMock(Storage.class); EasyMock.expect(blob1.getBlobId()).andReturn(BLOB_ID1).anyTimes(); EasyMock.expect(blob2.getBlobId()).andReturn(BLOB_ID2).anyTimes(); ArrayList<BlobId> ids = new ArrayList<>(); ids.add(BLOB_ID1); ids.add(BLOB_ID2); EasyMock.expect(storageMock.delete(ids)).andReturn(Collections.nCopies(2, true)).anyTimes(); EasyMock.expect(storageMock.list(BUCKET_NAME, BlobListOption.versions(true))) .andReturn(blobPage) .anyTimes(); EasyMock.expect(storageMock.delete(BUCKET_NAME)).andThrow(RETRYABLE_EXCEPTION).anyTimes(); EasyMock.replay(storageMock, blob1, blob2); assertFalse( RemoteStorageHelper.forceDelete(storageMock, BUCKET_NAME, 50, TimeUnit.MICROSECONDS)); EasyMock.verify(storageMock); }
@Test public void testListBlobsWithOptions() { String cursor = "cursor"; ImmutableList<BlobInfo> blobInfoList = ImmutableList.of(BLOB_INFO1, BLOB_INFO2); Tuple<String, Iterable<com.google.api.services.storage.model.StorageObject>> result = Tuple.of(cursor, Iterables.transform(blobInfoList, BlobInfo.INFO_TO_PB_FUNCTION)); EasyMock.expect(storageRpcMock.list(BUCKET_NAME1, BLOB_LIST_OPTIONS)).andReturn(result); EasyMock.replay(storageRpcMock); initializeService(); ImmutableList<Blob> blobList = ImmutableList.of(expectedBlob1, expectedBlob2); Page<Blob> page = storage.list(BUCKET_NAME1, BLOB_LIST_PAGE_SIZE, BLOB_LIST_PREFIX, BLOB_LIST_VERSIONS); assertEquals(cursor, page.getNextPageToken()); assertArrayEquals(blobList.toArray(), Iterables.toArray(page.getValues(), Blob.class)); }
@Test public void testListBucketsEmpty() { EasyMock.expect(storageRpcMock.list(EMPTY_RPC_OPTIONS)) .andReturn( Tuple.<String, Iterable<com.google.api.services.storage.model.Bucket>>of(null, null)); EasyMock.replay(storageRpcMock); initializeService(); Page<Bucket> page = storage.list(); assertNull(page.getNextPageToken()); assertArrayEquals( ImmutableList.of().toArray(), Iterables.toArray(page.getValues(), Bucket.class)); }
@Test public void testListBlobsEmpty() { EasyMock.expect(storageRpcMock.list(BUCKET_NAME1, EMPTY_RPC_OPTIONS)) .andReturn( Tuple.<String, Iterable<com.google.api.services.storage.model.StorageObject>>of( null, null)); EasyMock.replay(storageRpcMock); initializeService(); Page<Blob> page = storage.list(BUCKET_NAME1); assertNull(page.getNextPageToken()); assertArrayEquals( ImmutableList.of().toArray(), Iterables.toArray(page.getValues(), Blob.class)); }
@Test public void testList() throws Exception { initializeExpectedBucket(4); PageImpl<Blob> expectedBlobPage = new PageImpl<>(null, "c", blobResults); expect(storage.getOptions()).andReturn(mockOptions); expect(storage.list(BUCKET_INFO.getName())).andReturn(expectedBlobPage); replay(storage); initializeBucket(); Page<Blob> blobPage = bucket.list(); Iterator<Blob> blobInfoIterator = blobPage.getValues().iterator(); Iterator<Blob> blobIterator = blobPage.getValues().iterator(); while (blobInfoIterator.hasNext() && blobIterator.hasNext()) { assertEquals(blobInfoIterator.next(), blobIterator.next()); } assertFalse(blobInfoIterator.hasNext()); assertFalse(blobIterator.hasNext()); assertEquals(expectedBlobPage.getNextPageToken(), blobPage.getNextPageToken()); }
@Test public void testListBucketsWithEmptyFields() { String cursor = "cursor"; Capture<Map<StorageRpc.Option, Object>> capturedOptions = Capture.newInstance(); ImmutableList<BucketInfo> bucketInfoList = ImmutableList.of(BUCKET_INFO1, BUCKET_INFO2); Tuple<String, Iterable<com.google.api.services.storage.model.Bucket>> result = Tuple.of(cursor, Iterables.transform(bucketInfoList, BucketInfo.TO_PB_FUNCTION)); EasyMock.expect(storageRpcMock.list(EasyMock.capture(capturedOptions))).andReturn(result); EasyMock.replay(storageRpcMock); initializeService(); ImmutableList<Bucket> bucketList = ImmutableList.of(expectedBucket1, expectedBucket2); Page<Bucket> page = storage.list(BUCKET_LIST_EMPTY_FIELDS); String selector = (String) capturedOptions.getValue().get(BUCKET_LIST_EMPTY_FIELDS.getRpcOption()); assertTrue(selector.contains("items(")); assertTrue(selector.contains("name")); assertTrue(selector.contains("nextPageToken")); assertTrue(selector.endsWith(")")); assertEquals(25, selector.length()); assertEquals(cursor, page.getNextPageToken()); assertArrayEquals(bucketList.toArray(), Iterables.toArray(page.getValues(), Bucket.class)); }