@Override public BlobId apply(Blob blob) { return blob.getBlobId(); } });
@Override public BlobId apply(Blob blob) { return blob.getBlobId(); } });
/** * Updates an ACL entry on this blob. * * <p>Example of updating a new ACL entry. * * <pre>{@code * Acl acl = blob.updateAcl(Acl.of(User.ofAllAuthenticatedUsers(), Acl.Role.OWNER)); * }</pre> * * @throws StorageException upon failure */ public Acl updateAcl(Acl acl) { return storage.updateAcl(getBlobId(), acl); }
/** * Returns the ACL entry for the specified entity on this blob or {@code null} if not found. * * <p>Example of getting the ACL entry for an entity. * * <pre>{@code * Acl acl = blob.getAcl(User.ofAllAuthenticatedUsers()); * }</pre> * * @throws StorageException upon failure */ public Acl getAcl(Entity entity) { return storage.getAcl(getBlobId(), entity); }
/** * Creates a new ACL entry on this blob. * * <p>Example of creating a new ACL entry. * * <pre>{@code * Acl acl = blob.createAcl(Acl.of(User.ofAllAuthenticatedUsers(), Acl.Role.READER)); * }</pre> * * @throws StorageException upon failure */ public Acl createAcl(Acl acl) { return storage.createAcl(getBlobId(), acl); }
/** * Lists the ACL entries for this blob. * * <p>Example of listing the ACL entries. * * <pre>{@code * List<Acl> acls = blob.listAcls(); * for (Acl acl : acls) { * // do something with ACL entry * } * }</pre> * * @throws StorageException upon failure */ public List<Acl> listAcls() { return storage.listAcls(getBlobId()); }
/** * Returns this blob's content. * * <p>Example of reading all bytes of the blob, if its generation matches the {@link * Blob#getGeneration()} value, otherwise a {@link StorageException} is thrown. * * <pre>{@code * byte[] content = blob.getContent(BlobSourceOption.generationMatch()); * }</pre> * * @param options blob read options * @throws StorageException upon failure */ public byte[] getContent(BlobSourceOption... options) { return storage.readAllBytes(getBlobId(), toSourceOptions(this, options)); }
/** * Fetches current blob's latest information. Returns {@code null} if the blob does not exist. * * <p>Example of getting the blob's latest information, if its generation does not match the * {@link Blob#getGeneration()} value, otherwise a {@link StorageException} is thrown. * * <pre>{@code * Blob latestBlob = blob.reload(BlobSourceOption.generationNotMatch()); * if (latestBlob == null) { * // the blob was not found * } * }</pre> * * @param options blob read options * @return a {@code Blob} object with latest information or {@code null} if not found * @throws StorageException upon failure */ public Blob reload(BlobSourceOption... options) { return storage.get(getBlobId(), toGetOptions(this, options)); }
@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 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 testExists_True() throws Exception { initializeExpectedBlob(1); Storage.BlobGetOption[] expectedOptions = {Storage.BlobGetOption.fields()}; expect(storage.getOptions()).andReturn(mockOptions); expect(storage.get(expectedBlob.getBlobId(), expectedOptions)).andReturn(expectedBlob); replay(storage); initializeBlob(); assertTrue(blob.exists()); }
@Test public void testCopyToBucket() throws Exception { initializeExpectedBlob(2); BlobInfo target = BlobInfo.newBuilder(BlobId.of("bt", "n")).build(); CopyWriter copyWriter = createMock(CopyWriter.class); Capture<CopyRequest> capturedCopyRequest = Capture.newInstance(); expect(storage.getOptions()).andReturn(mockOptions); expect(storage.copy(capture(capturedCopyRequest))).andReturn(copyWriter); replay(storage); initializeBlob(); CopyWriter returnedCopyWriter = blob.copyTo("bt"); assertEquals(copyWriter, returnedCopyWriter); assertEquals(capturedCopyRequest.getValue().getSource(), blob.getBlobId()); assertEquals(capturedCopyRequest.getValue().getTarget(), target); assertFalse(capturedCopyRequest.getValue().overrideInfo()); assertTrue(capturedCopyRequest.getValue().getSourceOptions().isEmpty()); assertTrue(capturedCopyRequest.getValue().getTargetOptions().isEmpty()); }
@Test public void testCopyTo() throws Exception { initializeExpectedBlob(2); BlobInfo target = BlobInfo.newBuilder(BlobId.of("bt", "nt")).build(); CopyWriter copyWriter = createMock(CopyWriter.class); Capture<CopyRequest> capturedCopyRequest = Capture.newInstance(); expect(storage.getOptions()).andReturn(mockOptions); expect(storage.copy(capture(capturedCopyRequest))).andReturn(copyWriter); replay(storage); initializeBlob(); CopyWriter returnedCopyWriter = blob.copyTo("bt", "nt"); assertEquals(copyWriter, returnedCopyWriter); assertEquals(capturedCopyRequest.getValue().getSource(), blob.getBlobId()); assertEquals(capturedCopyRequest.getValue().getTarget(), target); assertFalse(capturedCopyRequest.getValue().overrideInfo()); assertTrue(capturedCopyRequest.getValue().getSourceOptions().isEmpty()); assertTrue(capturedCopyRequest.getValue().getTargetOptions().isEmpty()); }
@Test public void testCopyToBlobId() throws Exception { initializeExpectedBlob(2); BlobInfo target = BlobInfo.newBuilder(BlobId.of("bt", "nt")).build(); BlobId targetId = BlobId.of("bt", "nt"); CopyWriter copyWriter = createMock(CopyWriter.class); Capture<CopyRequest> capturedCopyRequest = Capture.newInstance(); expect(storage.getOptions()).andReturn(mockOptions); expect(storage.copy(capture(capturedCopyRequest))).andReturn(copyWriter); replay(storage); initializeBlob(); CopyWriter returnedCopyWriter = blob.copyTo(targetId); assertEquals(copyWriter, returnedCopyWriter); assertEquals(capturedCopyRequest.getValue().getSource(), blob.getBlobId()); assertEquals(capturedCopyRequest.getValue().getTarget(), target); assertFalse(capturedCopyRequest.getValue().overrideInfo()); assertTrue(capturedCopyRequest.getValue().getSourceOptions().isEmpty()); assertTrue(capturedCopyRequest.getValue().getTargetOptions().isEmpty()); }
@Test public void testGetBlobSelectedFields() { String blobName = "test-get-selected-fields-blob"; BlobInfo blob = BlobInfo.newBuilder(BUCKET, blobName) .setContentType(CONTENT_TYPE) .setMetadata(ImmutableMap.of("k", "v")) .build(); assertNotNull(storage.create(blob)); Blob remoteBlob = storage.get(blob.getBlobId(), Storage.BlobGetOption.fields(BlobField.METADATA)); assertEquals(blob.getBlobId(), remoteBlob.getBlobId()); assertEquals(ImmutableMap.of("k", "v"), remoteBlob.getMetadata()); assertNull(remoteBlob.getContentType()); }
@Test public void testGetBlobKmsKeyNameField() { String blobName = "test-get-selected-kms-key-name-field-blob"; BlobInfo blob = BlobInfo.newBuilder(BUCKET, blobName).setContentType(CONTENT_TYPE).build(); assertNotNull(storage.create(blob, Storage.BlobTargetOption.kmsKeyName(kmsKeyOneResourcePath))); Blob remoteBlob = storage.get(blob.getBlobId(), Storage.BlobGetOption.fields(BlobField.KMS_KEY_NAME)); assertEquals(blob.getBlobId(), remoteBlob.getBlobId()); assertTrue(remoteBlob.getKmsKeyName().startsWith(kmsKeyOneResourcePath)); assertNull(remoteBlob.getContentType()); }
@Test public void testGetBlobEmptySelectedFields() { String blobName = "test-get-empty-selected-fields-blob"; BlobInfo blob = BlobInfo.newBuilder(BUCKET, blobName).setContentType(CONTENT_TYPE).build(); assertNotNull(storage.create(blob)); Blob remoteBlob = storage.get(blob.getBlobId(), Storage.BlobGetOption.fields()); assertEquals(blob.getBlobId(), remoteBlob.getBlobId()); assertNull(remoteBlob.getContentType()); }
@Test public void testEnableDisableTemporaryHold() { String blobName = "test-create-with-temporary-hold"; BlobInfo blobInfo = BlobInfo.newBuilder(BUCKET, blobName).setTemporaryHold(true).build(); Blob remoteBlob = storage.create(blobInfo); assertTrue(remoteBlob.getTemporaryHold()); remoteBlob = storage.get(remoteBlob.getBlobId(), Storage.BlobGetOption.fields(BlobField.TEMPORARY_HOLD)); assertTrue(remoteBlob.getTemporaryHold()); remoteBlob = remoteBlob.toBuilder().setTemporaryHold(false).build().update(); assertFalse(remoteBlob.getTemporaryHold()); }