/** Example of reading all bytes of an encrypted blob. */ // [TARGET readAllBytes(BlobId, BlobSourceOption...)] // [VARIABLE "my_unique_bucket"] // [VARIABLE "my_blob_name"] // [VARIABLE "my_encryption_key"] public byte[] readEncryptedBlob(String bucketName, String blobName, String decryptionKey) { // [START readEncryptedBlob] byte[] content = storage.readAllBytes(bucketName, blobName, BlobSourceOption.decryptionKey(decryptionKey)); // [END readEncryptedBlob] return content; }
/** * Example of reading all bytes of a blob's specific generation, otherwise a {@link * StorageException} is thrown. */ // [TARGET readAllBytes(BlobId, BlobSourceOption...)] // [VARIABLE "my_unique_bucket"] // [VARIABLE "my_blob_name"] // [VARIABLE 42] public byte[] readBlobFromId(String bucketName, String blobName, long blobGeneration) { // [START readBlobFromId] BlobId blobId = BlobId.of(bucketName, blobName, blobGeneration); byte[] content = storage.readAllBytes(blobId); // [END readBlobFromId] return content; }
/** * Example of reading all bytes of a blob, if generation matches a value, otherwise a {@link * StorageException} is thrown. */ // [TARGET readAllBytes(String, String, BlobSourceOption...)] // [VARIABLE "my_unique_bucket"] // [VARIABLE "my_blob_name"] // [VARIABLE 42"] public byte[] readBlobFromStringsWithGeneration( String bucketName, String blobName, long blobGeneration) { // [START readBlobFromStringsWithGeneration] byte[] content = storage.readAllBytes( bucketName, blobName, BlobSourceOption.generationMatch(blobGeneration)); // [END readBlobFromStringsWithGeneration] return content; }
/** * 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)); }
@Override public InputStream read(UUID appId, Entity entity, long offset, long length) throws Exception { getService(); final byte[] content = instance.readAllBytes(BlobId.of(bucketName, AssetUtils.buildAssetKey( appId, entity ))); return new ByteArrayInputStream(content); }
@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 testReadAllBytes() { EasyMock.expect( storageRpcMock.load(BlobId.of(BUCKET_NAME1, BLOB_NAME1).toPb(), EMPTY_RPC_OPTIONS)) .andReturn(BLOB_CONTENT); EasyMock.replay(storageRpcMock); initializeService(); byte[] readBytes = storage.readAllBytes(BUCKET_NAME1, BLOB_NAME1); assertArrayEquals(BLOB_CONTENT, readBytes); }
@Test public void testReadAllBytesWithOptions() { EasyMock.expect( storageRpcMock.load(BlobId.of(BUCKET_NAME1, BLOB_NAME1).toPb(), BLOB_SOURCE_OPTIONS)) .andReturn(BLOB_CONTENT); EasyMock.replay(storageRpcMock); initializeService(); byte[] readBytes = storage.readAllBytes( BUCKET_NAME1, BLOB_NAME1, BLOB_SOURCE_GENERATION, BLOB_SOURCE_METAGENERATION); assertArrayEquals(BLOB_CONTENT, readBytes); }
@Test public void testReadAllBytesFromBlobIdWithOptions() { EasyMock.expect(storageRpcMock.load(BLOB_INFO1.getBlobId().toPb(), BLOB_SOURCE_OPTIONS)) .andReturn(BLOB_CONTENT); EasyMock.replay(storageRpcMock); initializeService(); byte[] readBytes = storage.readAllBytes( BLOB_INFO1.getBlobId(), BLOB_SOURCE_GENERATION_FROM_BLOB_ID, BLOB_SOURCE_METAGENERATION); assertArrayEquals(BLOB_CONTENT, readBytes); }
@Test public void testReadAllBytesWithDecriptionKey() { EasyMock.expect( storageRpcMock.load(BlobId.of(BUCKET_NAME1, BLOB_NAME1).toPb(), ENCRYPTION_KEY_OPTIONS)) .andReturn(BLOB_CONTENT) .times(2); EasyMock.replay(storageRpcMock); initializeService(); byte[] readBytes = storage.readAllBytes(BUCKET_NAME1, BLOB_NAME1, BlobSourceOption.decryptionKey(KEY)); assertArrayEquals(BLOB_CONTENT, readBytes); readBytes = storage.readAllBytes(BUCKET_NAME1, BLOB_NAME1, BlobSourceOption.decryptionKey(BASE64_KEY)); assertArrayEquals(BLOB_CONTENT, readBytes); }
@Test public void testReadAllBytesFromBlobIdWithDecriptionKey() { EasyMock.expect(storageRpcMock.load(BLOB_INFO1.getBlobId().toPb(), ENCRYPTION_KEY_OPTIONS)) .andReturn(BLOB_CONTENT) .times(2); EasyMock.replay(storageRpcMock); initializeService(); byte[] readBytes = storage.readAllBytes(BLOB_INFO1.getBlobId(), BlobSourceOption.decryptionKey(KEY)); assertArrayEquals(BLOB_CONTENT, readBytes); readBytes = storage.readAllBytes(BLOB_INFO1.getBlobId(), BlobSourceOption.decryptionKey(BASE64_KEY)); assertArrayEquals(BLOB_CONTENT, readBytes); }
@Test public void testContentWithDecryptionKey() throws Exception { initializeExpectedBlob(2); byte[] content = {1, 2}; expect(storage.getOptions()).andReturn(mockOptions); expect( storage.readAllBytes( BLOB_INFO.getBlobId(), Storage.BlobSourceOption.decryptionKey(BASE64_KEY))) .andReturn(content) .times(2); replay(storage); initializeBlob(); assertArrayEquals(content, blob.getContent(BlobSourceOption.decryptionKey(BASE64_KEY))); assertArrayEquals(content, blob.getContent(BlobSourceOption.decryptionKey(KEY))); }
@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 testCreateBlobStream() { String blobName = "test-create-blob-stream"; BlobInfo blob = BlobInfo.newBuilder(BUCKET, blobName).setContentType(CONTENT_TYPE).build(); ByteArrayInputStream stream = new ByteArrayInputStream(BLOB_STRING_CONTENT.getBytes(UTF_8)); Blob remoteBlob = storage.create(blob, stream); assertNotNull(remoteBlob); assertEquals(blob.getBucket(), remoteBlob.getBucket()); assertEquals(blob.getName(), remoteBlob.getName()); assertEquals(blob.getContentType(), remoteBlob.getContentType()); byte[] readBytes = storage.readAllBytes(BUCKET, blobName); assertEquals(BLOB_STRING_CONTENT, new String(readBytes, UTF_8)); }
@Test public void testCreateEmptyBlob() { String blobName = "test-create-empty-blob"; BlobInfo blob = BlobInfo.newBuilder(BUCKET, blobName).build(); Blob remoteBlob = storage.create(blob); assertNotNull(remoteBlob); assertEquals(blob.getBucket(), remoteBlob.getBucket()); assertEquals(blob.getName(), remoteBlob.getName()); byte[] readBytes = storage.readAllBytes(BUCKET, blobName); assertArrayEquals(new byte[0], readBytes); }
@Test public void testCreateBlobWithKmsKeyName() { String blobName = "test-create-with-kms-key-name-blob"; BlobInfo blob = BlobInfo.newBuilder(BUCKET, blobName).build(); Blob remoteBlob = storage.create( blob, BLOB_BYTE_CONTENT, Storage.BlobTargetOption.kmsKeyName(kmsKeyOneResourcePath)); assertNotNull(remoteBlob); assertEquals(blob.getBucket(), remoteBlob.getBucket()); assertEquals(blob.getName(), remoteBlob.getName()); assertNotNull(remoteBlob.getKmsKeyName()); assertTrue(remoteBlob.getKmsKeyName().startsWith(kmsKeyOneResourcePath)); byte[] readBytes = storage.readAllBytes(BUCKET, blobName); assertArrayEquals(BLOB_BYTE_CONTENT, readBytes); }
@Test public void testComposeBlob() { String sourceBlobName1 = "test-compose-blob-source-1"; String sourceBlobName2 = "test-compose-blob-source-2"; BlobInfo sourceBlob1 = BlobInfo.newBuilder(BUCKET, sourceBlobName1).build(); BlobInfo sourceBlob2 = BlobInfo.newBuilder(BUCKET, sourceBlobName2).build(); Blob remoteSourceBlob1 = storage.create(sourceBlob1, BLOB_BYTE_CONTENT); Blob remoteSourceBlob2 = storage.create(sourceBlob2, BLOB_BYTE_CONTENT); assertNotNull(remoteSourceBlob1); assertNotNull(remoteSourceBlob2); String targetBlobName = "test-compose-blob-target"; BlobInfo targetBlob = BlobInfo.newBuilder(BUCKET, targetBlobName).build(); Storage.ComposeRequest req = Storage.ComposeRequest.of(ImmutableList.of(sourceBlobName1, sourceBlobName2), targetBlob); Blob remoteTargetBlob = storage.compose(req); assertNotNull(remoteTargetBlob); assertEquals(targetBlob.getName(), remoteTargetBlob.getName()); assertEquals(targetBlob.getBucket(), remoteTargetBlob.getBucket()); assertNull(remoteTargetBlob.getContentType()); byte[] readBytes = storage.readAllBytes(BUCKET, targetBlobName); byte[] composedBytes = Arrays.copyOf(BLOB_BYTE_CONTENT, BLOB_BYTE_CONTENT.length * 2); System.arraycopy( BLOB_BYTE_CONTENT, 0, composedBytes, BLOB_BYTE_CONTENT.length, BLOB_BYTE_CONTENT.length); assertArrayEquals(composedBytes, readBytes); }
@Test public void testCreateBlob() { String blobName = "test-create-blob"; BlobInfo blob = BlobInfo.newBuilder(BUCKET, blobName).build(); Blob remoteBlob = storage.create(blob, BLOB_BYTE_CONTENT); assertNotNull(remoteBlob); assertEquals(blob.getBucket(), remoteBlob.getBucket()); assertEquals(blob.getName(), remoteBlob.getName()); byte[] readBytes = storage.readAllBytes(BUCKET, blobName); assertArrayEquals(BLOB_BYTE_CONTENT, readBytes); assertTrue(remoteBlob.delete()); }
@Test public void testCreateBlobWithEncryptionKey() { String blobName = "test-create-with-customer-key-blob"; BlobInfo blob = BlobInfo.newBuilder(BUCKET, blobName).build(); Blob remoteBlob = storage.create(blob, BLOB_BYTE_CONTENT, Storage.BlobTargetOption.encryptionKey(KEY)); assertNotNull(remoteBlob); assertEquals(blob.getBucket(), remoteBlob.getBucket()); assertEquals(blob.getName(), remoteBlob.getName()); byte[] readBytes = storage.readAllBytes(BUCKET, blobName, Storage.BlobSourceOption.decryptionKey(BASE64_KEY)); assertArrayEquals(BLOB_BYTE_CONTENT, readBytes); }
@Test public void testUpdateBucketRequesterPays() { Bucket remoteBucket = storage.get(BUCKET, Storage.BucketGetOption.fields(BucketField.ID)); assertNull(remoteBucket.requesterPays()); remoteBucket = remoteBucket.toBuilder().setRequesterPays(true).build(); Bucket updatedBucket = storage.update(remoteBucket); assertTrue(updatedBucket.requesterPays()); String projectId = remoteStorageHelper.getOptions().getProjectId(); Bucket.BlobTargetOption option = Bucket.BlobTargetOption.userProject(projectId); String blobName = "test-create-empty-blob-requester-pays"; Blob remoteBlob = updatedBucket.create(blobName, BLOB_BYTE_CONTENT, option); assertNotNull(remoteBlob); byte[] readBytes = storage.readAllBytes(BUCKET, blobName); assertArrayEquals(BLOB_BYTE_CONTENT, readBytes); }