@Override public void run(Storage storage, BlobId... blobIds) { // use batch operation List<Boolean> deleteResults = storage.delete(blobIds); int index = 0; for (Boolean deleted : deleteResults) { if (deleted) { // request order is maintained System.out.printf("Blob %s was deleted%n", blobIds[index]); } index++; } } }
/** Example of deleting a blob. */ // [TARGET delete(BlobId)] // [VARIABLE "my_unique_bucket"] // [VARIABLE "my_blob_name"] public boolean deleteBlob(String bucketName, String blobName) { // [START deleteBlob] BlobId blobId = BlobId.of(bucketName, blobName); boolean deleted = storage.delete(blobId); if (deleted) { // the blob was deleted } else { // the blob was not found } // [END deleteBlob] return deleted; }
/** Example of deleting several blobs using a single batch request. */ // [TARGET delete(Iterable)] // [VARIABLE "my_unique_bucket"] // [VARIABLE "my_blob_name1"] // [VARIABLE "my_blob_name2"] public List<Boolean> batchDeleteIterable(String bucketName, String blobName1, String blobName2) { // [START batchDeleteIterable] List<BlobId> blobIds = new LinkedList<>(); blobIds.add(BlobId.of(bucketName, blobName1)); blobIds.add(BlobId.of(bucketName, blobName2)); List<Boolean> deleted = storage.delete(blobIds); // [END batchDeleteIterable] return deleted; }
/** Example of deleting several blobs using a single batch request. */ // [TARGET delete(BlobId...)] // [VARIABLE "my_unique_bucket"] // [VARIABLE "my_blob_name1"] // [VARIABLE "my_blob_name2"] public List<Boolean> batchDelete(String bucketName, String blobName1, String blobName2) { // [START batchDelete] BlobId firstBlob = BlobId.of(bucketName, blobName1); BlobId secondBlob = BlobId.of(bucketName, blobName2); List<Boolean> deleted = storage.delete(firstBlob, secondBlob); // [END batchDelete] return deleted; }
@Override public void delete(UUID appId, Entity entity) throws Exception { getService(); final BlobId blobId = BlobId.of(bucketName, AssetUtils.buildAssetKey( appId, entity )); instance.delete(blobId); }
@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 testDeleteNonExistingBlob() { String blobName = "test-delete-non-existing-blob"; assertFalse(storage.delete(BUCKET, blobName)); }
@Test public void testDelete() throws Exception { initializeExpectedBlob(2); expect(storage.getOptions()).andReturn(mockOptions); expect(storage.delete(BLOB_INFO.getBlobId(), new Storage.BlobSourceOption[0])).andReturn(true); replay(storage); initializeBlob(); assertTrue(blob.delete()); }
@Test public void testDelete() throws Exception { initializeExpectedBucket(4); expect(storage.getOptions()).andReturn(mockOptions); expect(storage.delete(BUCKET_INFO.getName())).andReturn(true); replay(storage); initializeBucket(); assertTrue(bucket.delete()); }
@Test public void testDeleteBlob() { EasyMock.expect( storageRpcMock.delete(BlobId.of(BUCKET_NAME1, BLOB_NAME1).toPb(), EMPTY_RPC_OPTIONS)) .andReturn(true); EasyMock.replay(storageRpcMock); initializeService(); assertTrue(storage.delete(BUCKET_NAME1, BLOB_NAME1)); }
@Test public void testDeleteBucketWithOptions() { EasyMock.expect( storageRpcMock.delete(BucketInfo.of(BUCKET_NAME1).toPb(), BUCKET_SOURCE_OPTIONS)) .andReturn(true); EasyMock.replay(storageRpcMock); initializeService(); assertTrue(storage.delete(BUCKET_NAME1, BUCKET_SOURCE_METAGENERATION)); }
@Test public void testDeleteBlobWithOptions() { EasyMock.expect( storageRpcMock.delete(BlobId.of(BUCKET_NAME1, BLOB_NAME1).toPb(), BLOB_SOURCE_OPTIONS)) .andReturn(true); EasyMock.replay(storageRpcMock); initializeService(); assertTrue( storage.delete( BUCKET_NAME1, BLOB_NAME1, BLOB_SOURCE_GENERATION, BLOB_SOURCE_METAGENERATION)); }
@Test public void testDeleteBucket() { EasyMock.expect(storageRpcMock.delete(BucketInfo.of(BUCKET_NAME1).toPb(), EMPTY_RPC_OPTIONS)) .andReturn(true); EasyMock.replay(storageRpcMock); initializeService(); assertTrue(storage.delete(BUCKET_NAME1)); }
@Test public void testDeleteBlobWithOptionsFromBlobId() { EasyMock.expect(storageRpcMock.delete(BLOB_INFO1.getBlobId().toPb(), BLOB_SOURCE_OPTIONS)) .andReturn(true); EasyMock.replay(storageRpcMock); initializeService(); assertTrue( storage.delete( BLOB_INFO1.getBlobId(), BLOB_SOURCE_GENERATION_FROM_BLOB_ID, BLOB_SOURCE_METAGENERATION)); }
@Test public void testWriteChannelExistingBlob() throws IOException { String blobName = "test-write-channel-existing-blob"; BlobInfo blob = BlobInfo.newBuilder(BUCKET, blobName).build(); storage.create(blob); byte[] stringBytes; try (WriteChannel writer = storage.writer(blob)) { stringBytes = BLOB_STRING_CONTENT.getBytes(UTF_8); writer.write(ByteBuffer.wrap(stringBytes)); } assertArrayEquals(stringBytes, storage.readAllBytes(blob.getBlobId())); assertTrue(storage.delete(BUCKET, blobName)); }
@Test public void testDeleteBlobsFail() { String sourceBlobName1 = "test-delete-blobs-fail-1"; String sourceBlobName2 = "test-delete-blobs-fail-2"; BlobInfo sourceBlob1 = BlobInfo.newBuilder(BUCKET, sourceBlobName1).build(); BlobInfo sourceBlob2 = BlobInfo.newBuilder(BUCKET, sourceBlobName2).build(); assertNotNull(storage.create(sourceBlob1)); List<Boolean> deleteStatus = storage.delete(sourceBlob1.getBlobId(), sourceBlob2.getBlobId()); assertTrue(deleteStatus.get(0)); assertFalse(deleteStatus.get(1)); }
@Test public void testDeleteBlobs() { String sourceBlobName1 = "test-delete-blobs-1"; String sourceBlobName2 = "test-delete-blobs-2"; BlobInfo sourceBlob1 = BlobInfo.newBuilder(BUCKET, sourceBlobName1).build(); BlobInfo sourceBlob2 = BlobInfo.newBuilder(BUCKET, sourceBlobName2).build(); assertNotNull(storage.create(sourceBlob1)); assertNotNull(storage.create(sourceBlob2)); List<Boolean> deleteStatus = storage.delete(sourceBlob1.getBlobId(), sourceBlob2.getBlobId()); assertTrue(deleteStatus.get(0)); assertTrue(deleteStatus.get(1)); }
@Test public void testDeleteBlobNonExistingGeneration() { String blobName = "test-delete-blob-non-existing-generation"; BlobInfo blob = BlobInfo.newBuilder(BUCKET, blobName).build(); assertNotNull(storage.create(blob)); try { assertFalse(storage.delete(BlobId.of(BUCKET, blobName, -1L))); fail("Expected an 'Invalid argument' exception"); } catch (StorageException e) { assertThat(e.getMessage()).contains("Invalid argument"); } }
@Test public void testDeleteBlobFail() { String blobName = "test-delete-blob-fail"; BlobInfo blob = BlobInfo.newBuilder(BUCKET, blobName).build(); Blob remoteBlob = storage.create(blob); assertNotNull(remoteBlob); try { storage.delete(BUCKET, blob.getName(), Storage.BlobSourceOption.generationMatch(-1L)); fail("StorageException was expected"); } catch (StorageException ex) { // expected } assertTrue(remoteBlob.delete()); }