Refine search
@Before public void before() throws IOException { when(metadata.getSize()).thenReturn(42L); when(metadata.getGeneration()).thenReturn(2L); when(gcsStorage.get( file, Storage.BlobGetOption.fields(Storage.BlobField.GENERATION, Storage.BlobField.SIZE))) .thenReturn(metadata); when(gcsStorage.reader(file, Storage.BlobSourceOption.generationMatch(2L))) .thenReturn(gcsChannel); when(gcsChannel.isOpen()).thenReturn(true); chan = CloudStorageReadChannel.create( gcsStorage, file, 0, 1, CloudStorageConfiguration.DEFAULT, ""); verify(gcsStorage) .get( eq(file), eq(Storage.BlobGetOption.fields(Storage.BlobField.GENERATION, Storage.BlobField.SIZE))); verify(gcsStorage).reader(eq(file), eq(Storage.BlobSourceOption.generationMatch(2L))); }
@Test public void testReadAndWriteChannelWithEncryptionKey() throws IOException { String blobName = "test-read-write-channel-with-customer-key-blob"; BlobInfo blob = BlobInfo.newBuilder(BUCKET, blobName).build(); byte[] stringBytes; try (WriteChannel writer = storage.writer(blob, Storage.BlobWriteOption.encryptionKey(BASE64_KEY))) { stringBytes = BLOB_STRING_CONTENT.getBytes(UTF_8); writer.write(ByteBuffer.wrap(BLOB_BYTE_CONTENT)); writer.write(ByteBuffer.wrap(stringBytes)); } ByteBuffer readBytes; ByteBuffer readStringBytes; try (ReadChannel reader = storage.reader(blob.getBlobId(), Storage.BlobSourceOption.decryptionKey(KEY))) { readBytes = ByteBuffer.allocate(BLOB_BYTE_CONTENT.length); readStringBytes = ByteBuffer.allocate(stringBytes.length); reader.read(readBytes); reader.read(readStringBytes); } assertArrayEquals(BLOB_BYTE_CONTENT, readBytes.array()); assertEquals(BLOB_STRING_CONTENT, new String(readStringBytes.array(), UTF_8)); assertTrue(storage.delete(BUCKET, blobName)); }
Page<Blob> listedBlobs; if (Strings.isNullOrEmpty(userProject)) { listedBlobs = storage.list(bucket, BlobListOption.versions(true)); } else { listedBlobs = storage.list( bucket, BlobListOption.versions(true), BlobListOption.userProject(userProject)); List<Boolean> results = storage.delete(ids); if (!Strings.isNullOrEmpty(userProject)) { for (int i = 0; i < results.size(); i++) { bucket, ids.get(i).getName(), Storage.BlobSourceOption.userProject(userProject));
ImmutableMap<String, String> metadata = ImmutableMap.of("k", "v"); Blob remoteBlob = storage.create( BlobInfo.newBuilder(source).build(), BLOB_BYTE_CONTENT, Storage.CopyRequest.newBuilder() .setSource(source) .setSourceOptions(Storage.BlobSourceOption.decryptionKey(BASE64_KEY)) .setTarget(target, Storage.BlobTargetOption.kmsKeyName(kmsKeyOneResourcePath)) .build(); CopyWriter copyWriter = storage.copy(req); assertEquals(BUCKET, copyWriter.getResult().getBucket()); assertEquals(targetBlobName, copyWriter.getResult().getName()); assertEquals(metadata, copyWriter.getResult().getMetadata()); assertTrue(copyWriter.isDone()); assertTrue(storage.delete(BUCKET, targetBlobName));
ImmutableMap<String, String> metadata = ImmutableMap.of("k", "v"); Blob remoteBlob = storage.create( BlobInfo.newBuilder(source).build(), BLOB_BYTE_CONTENT, Storage.CopyRequest.newBuilder() .setSource(source) .setSourceOptions(Storage.BlobSourceOption.decryptionKey(BASE64_KEY)) .setTarget( target, Storage.BlobTargetOption.kmsKeyName(kmsKeyOneResourcePath)) .build(); storage.copy(req); fail("StorageException was expected"); } catch (StorageException ex) {
@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()); }
@Test public void testReadChannelFail() throws IOException { String blobName = "test-read-channel-blob-fail"; BlobInfo blob = BlobInfo.newBuilder(BUCKET, blobName).build(); Blob remoteBlob = storage.create(blob); assertNotNull(remoteBlob); try (ReadChannel reader = storage.reader(blob.getBlobId(), Storage.BlobSourceOption.metagenerationMatch(-1L))) { reader.read(ByteBuffer.allocate(42)); fail("StorageException was expected"); } catch (StorageException ex) { // expected } try (ReadChannel reader = storage.reader(blob.getBlobId(), Storage.BlobSourceOption.generationMatch(-1L))) { reader.read(ByteBuffer.allocate(42)); fail("StorageException was expected"); } catch (StorageException ex) { // expected } BlobId blobIdWrongGeneration = BlobId.of(BUCKET, blobName, -1L); try (ReadChannel reader = storage.reader(blobIdWrongGeneration, Storage.BlobSourceOption.generationMatch())) { reader.read(ByteBuffer.allocate(42)); fail("StorageException was expected"); } catch (StorageException ex) { // expected } }
@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); }
BlobInfo blobInfo; if (isNullOrEmpty(userProject)) { blobInfo = storage.get(fromPath.getBlobId()); } else { blobInfo = storage.get(fromPath.getBlobId(), BlobGetOption.userProject(userProject)); copyReqBuilder = copyReqBuilder.setSourceOptions( BlobSourceOption.userProject(fromPath.getFileSystem().config().userProject())); } else if (!isNullOrEmpty(toPath.getFileSystem().config().userProject())) { copyReqBuilder = copyReqBuilder.setSourceOptions( BlobSourceOption.userProject(toPath.getFileSystem().config().userProject())); CopyWriter copyWriter = storage.copy(copyReqBuilder.build()); copyWriter.getResult(); break;
String blobName = "test-batch-request-blob-fail"; BlobInfo blob = BlobInfo.newBuilder(BUCKET, blobName).build(); Blob remoteBlob = storage.create(blob); assertNotNull(remoteBlob); BlobInfo updatedBlob = BlobInfo.newBuilder(BUCKET, blobName, -1L).build(); StorageBatch batch = storage.batch(); StorageBatchResult<Blob> updateResult = batch.update(updatedBlob, Storage.BlobTargetOption.generationMatch()); StorageBatchResult<Boolean> deleteResult1 = batch.delete(BUCKET, blobName, Storage.BlobSourceOption.generationMatch(-1L)); StorageBatchResult<Boolean> deleteResult2 = batch.delete(BlobId.of(BUCKET, blobName, -1L)); StorageBatchResult<Blob> getResult1 =
/** * Example of deleting a blob, only if its generation matches a value, otherwise a {@link * StorageException} is thrown. */ // [TARGET delete(String, String, BlobSourceOption...)] // [VARIABLE "my_unique_bucket"] // [VARIABLE "my_blob_name"] // [VARIABLE 42] public boolean deleteBlobFromStringsWithGeneration( String bucketName, String blobName, long blobGeneration) { // [START deleteBlobFromStringsWithGeneration] boolean deleted = storage.delete(bucketName, blobName, BlobSourceOption.generationMatch(blobGeneration)); if (deleted) { // the blob was deleted } else { // the blob was not found } // [END deleteBlobFromStringsWithGeneration] return deleted; }
@Test public void testReaderWithDecryptionKey() throws Exception { initializeExpectedBlob(2); ReadChannel channel = createMock(ReadChannel.class); expect(storage.getOptions()).andReturn(mockOptions); expect( storage.reader( BLOB_INFO.getBlobId(), Storage.BlobSourceOption.decryptionKey(BASE64_KEY))) .andReturn(channel) .times(2); replay(storage); initializeBlob(); assertSame(channel, blob.reader(BlobSourceOption.decryptionKey(BASE64_KEY))); assertSame(channel, blob.reader(BlobSourceOption.decryptionKey(KEY))); }
/** 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; }
@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))); }
/** Example of rotating the encryption key of a blob. */ // [TARGET copy(CopyRequest)] // [VARIABLE "my_unique_bucket"] // [VARIABLE "my_blob_name"] // [VARIABLE "old_encryption_key"] // [VARIABLE "new_encryption_key"] public Blob rotateBlobEncryptionKey( String bucketName, String blobName, String oldEncryptionKey, String newEncryptionKey) { // [START storageRotateEncryptionKey] BlobId blobId = BlobId.of(bucketName, blobName); CopyRequest request = CopyRequest.newBuilder() .setSource(blobId) .setSourceOptions(BlobSourceOption.decryptionKey(oldEncryptionKey)) .setTarget(blobId, BlobTargetOption.encryptionKey(newEncryptionKey)) .build(); Blob blob = storage.copy(request).getResult(); // [END storageRotateEncryptionKey] return blob; }
@Test public void testReaderWithDecryptionKey() throws IOException { byte[] result = new byte[DEFAULT_CHUNK_SIZE]; EasyMock.expect( storageRpcMock.read(BLOB_INFO2.toPb(), ENCRYPTION_KEY_OPTIONS, 0, DEFAULT_CHUNK_SIZE)) .andReturn(Tuple.of("etag", result)) .times(2); EasyMock.replay(storageRpcMock); initializeService(); ReadChannel channel = storage.reader(BUCKET_NAME1, BLOB_NAME2, BlobSourceOption.decryptionKey(KEY)); assertNotNull(channel); assertTrue(channel.isOpen()); channel.read(ByteBuffer.allocate(42)); channel = storage.reader(BUCKET_NAME1, BLOB_NAME2, BlobSourceOption.decryptionKey(BASE64_KEY)); assertNotNull(channel); assertTrue(channel.isOpen()); channel.read(ByteBuffer.allocate(42)); }
/** * Returns an option for blob's data generation match. If this option is used the request will * fail if blob's generation does not match. The generation value to compare with the actual * blob's generation is taken from a source {@link BlobId} object. When this option is passed to * a {@link Storage} method and {@link BlobId#getGeneration()} is {@code null} or no {@link * BlobId} is provided an exception is thrown. */ public static BlobSourceOption generationMatch() { return new BlobSourceOption(StorageRpc.Option.IF_GENERATION_MATCH, null); }
/** * Returns an option for blob's data generation match. If this option is used the request will * fail if blob's generation does not match the provided value. */ public static BlobSourceOption generationMatch(long generation) { return new BlobSourceOption(StorageRpc.Option.IF_GENERATION_MATCH, generation); }
/** * Returns an option to set a customer-supplied AES256 key for server-side encryption of the * blob. */ public static BlobSourceOption decryptionKey(Key key) { String base64Key = BaseEncoding.base64().encode(key.getEncoded()); return new BlobSourceOption(StorageRpc.Option.CUSTOMER_SUPPLIED_KEY, base64Key); }
/** * Returns an option to set a customer-supplied AES256 key for server-side encryption of the * blob. * * @param key the AES256 encoded in base64 */ public static BlobSourceOption decryptionKey(String key) { return new BlobSourceOption(StorageRpc.Option.CUSTOMER_SUPPLIED_KEY, key); }