/** Returns the name of the containing bucket. */ public String getBucket() { return getBlobId().getBucket(); }
private void run(Storage storage, ServiceAccountCredentials cred, BlobInfo blobInfo) { Blob blob = storage.get(blobInfo.getBlobId()); System.out.printf( "Signed URL: %s%n", blob.signUrl(1, TimeUnit.DAYS, SignUrlOption.signWith(cred))); }
@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 testGetBlobWithOptionsFromBlobId() { EasyMock.expect(storageRpcMock.get(BLOB_INFO1.getBlobId().toPb(), BLOB_GET_OPTIONS)) .andReturn(BLOB_INFO1.toPb()); EasyMock.replay(storageRpcMock); initializeService(); Blob blob = storage.get( BLOB_INFO1.getBlobId(), BLOB_GET_METAGENERATION, BLOB_GET_GENERATION_FROM_BLOB_ID); assertEquals(expectedBlob1, blob); }
@Test public void testReader() throws Exception { initializeExpectedBlob(2); ReadChannel channel = createMock(ReadChannel.class); expect(storage.getOptions()).andReturn(mockOptions); expect(storage.reader(BLOB_INFO.getBlobId())).andReturn(channel); replay(storage); initializeBlob(); assertSame(channel, blob.reader()); }
@Test public void testReloadNull() throws Exception { initializeExpectedBlob(1); expect(storage.getOptions()).andReturn(mockOptions); expect(storage.get(BLOB_INFO.getBlobId(), new Storage.BlobGetOption[0])).andReturn(null); replay(storage); initializeBlob(); Blob reloadedBlob = blob.reload(); assertNull(reloadedBlob); }
@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 testListAcls() throws Exception { initializeExpectedBlob(1); expect(storage.getOptions()).andReturn(mockOptions); expect(storage.listAcls(BLOB_INFO.getBlobId())).andReturn(ACLS); replay(storage); initializeBlob(); assertEquals(ACLS, blob.listAcls()); }
@Test public void testContent() throws Exception { initializeExpectedBlob(2); byte[] content = {1, 2}; expect(storage.getOptions()).andReturn(mockOptions); expect(storage.readAllBytes(BLOB_INFO.getBlobId())).andReturn(content); replay(storage); initializeBlob(); assertArrayEquals(content, blob.getContent()); }
@Test public void testExists_False() throws Exception { Storage.BlobGetOption[] expectedOptions = {Storage.BlobGetOption.fields()}; expect(storage.getOptions()).andReturn(null); expect(storage.get(BLOB_INFO.getBlobId(), expectedOptions)).andReturn(null); replay(storage); initializeBlob(); assertFalse(blob.exists()); }
@Test public void testBlobId() { assertEquals(BlobId.of("b", "n", GENERATION), BLOB_INFO.getBlobId()); } }
@Test public void testDeleteAcl() throws Exception { initializeExpectedBlob(1); expect(storage.getOptions()).andReturn(mockOptions); expect(storage.deleteAcl(BLOB_INFO.getBlobId(), User.ofAllAuthenticatedUsers())) .andReturn(true); replay(storage); initializeBlob(); assertTrue(blob.deleteAcl(User.ofAllAuthenticatedUsers())); }
@Test public void testGetAcl() throws Exception { initializeExpectedBlob(1); expect(storage.getOptions()).andReturn(mockOptions); expect(storage.getAcl(BLOB_INFO.getBlobId(), User.ofAllAuthenticatedUsers())).andReturn(ACL); replay(storage); initializeBlob(); assertEquals(ACL, blob.getAcl(User.ofAllAuthenticatedUsers())); }
@Test public void testReload() throws Exception { initializeExpectedBlob(2); Blob expectedReloadedBlob = expectedBlob.toBuilder().setCacheControl("c").build(); expect(storage.getOptions()).andReturn(mockOptions); expect(storage.get(BLOB_INFO.getBlobId(), new Storage.BlobGetOption[0])) .andReturn(expectedReloadedBlob); replay(storage); initializeBlob(); Blob updatedBlob = blob.reload(); assertEquals(expectedReloadedBlob, updatedBlob); }
@Test public void testCreateAcl() throws Exception { initializeExpectedBlob(1); expect(storage.getOptions()).andReturn(mockOptions); Acl returnedAcl = ACL.toBuilder().setEtag("ETAG").setId("ID").build(); expect(storage.createAcl(BLOB_INFO.getBlobId(), ACL)).andReturn(returnedAcl); replay(storage); initializeBlob(); assertEquals(returnedAcl, blob.createAcl(ACL)); }
@Test public void testUpdateAcl() throws Exception { initializeExpectedBlob(1); expect(storage.getOptions()).andReturn(mockOptions); Acl returnedAcl = ACL.toBuilder().setEtag("ETAG").setId("ID").build(); expect(storage.updateAcl(BLOB_INFO.getBlobId(), ACL)).andReturn(returnedAcl); replay(storage); initializeBlob(); assertEquals(returnedAcl, blob.updateAcl(ACL)); }
@Test public void testReloadWithOptions() throws Exception { initializeExpectedBlob(2); Blob expectedReloadedBlob = expectedBlob.toBuilder().setCacheControl("c").build(); Storage.BlobGetOption[] options = {Storage.BlobGetOption.metagenerationMatch(42L)}; expect(storage.getOptions()).andReturn(mockOptions); expect(storage.get(BLOB_INFO.getBlobId(), options)).andReturn(expectedReloadedBlob); replay(storage); initializeBlob(); Blob updatedBlob = blob.reload(BlobSourceOption.metagenerationMatch()); assertEquals(expectedReloadedBlob, updatedBlob); }
@Test public void testGetBlobsFail() { String sourceBlobName1 = "test-get-blobs-fail-1"; String sourceBlobName2 = "test-get-blobs-fail-2"; BlobInfo sourceBlob1 = BlobInfo.newBuilder(BUCKET, sourceBlobName1).build(); BlobInfo sourceBlob2 = BlobInfo.newBuilder(BUCKET, sourceBlobName2).build(); assertNotNull(storage.create(sourceBlob1)); List<Blob> remoteBlobs = storage.get(sourceBlob1.getBlobId(), sourceBlob2.getBlobId()); assertEquals(sourceBlob1.getBucket(), remoteBlobs.get(0).getBucket()); assertEquals(sourceBlob1.getName(), remoteBlobs.get(0).getName()); assertNull(remoteBlobs.get(1)); }
@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()); }