4444import com .google .cloud .storage .Storage .BlobListOption ;
4545import com .google .cloud .storage .Storage .BlobSourceOption ;
4646import com .google .cloud .storage .Storage .BlobTargetOption ;
47+ import com .google .cloud .storage .Storage .BlobWriteOption ;
4748import com .google .cloud .storage .Storage .BucketGetOption ;
4849import com .google .cloud .storage .Storage .BucketListOption ;
4950import com .google .cloud .storage .Storage .BucketSourceOption ;
@@ -153,6 +154,26 @@ public Blob createBlobFromInputStream(String bucketName, String blobName) {
153154 return blob ;
154155 }
155156
157+ /**
158+ * Example of uploading an encrypted blob.
159+ */
160+ // [TARGET create(BlobInfo, InputStream, BlobWriteOption...)]
161+ // [VARIABLE "my_unique_bucket"]
162+ // [VARIABLE "my_blob_name"]
163+ // [VARIABLE "my_encryption_key"]
164+ public Blob createEncryptedBlob (String bucketName , String blobName , String encryptionKey ) {
165+ // [START storageUploadEncryptedFile]
166+ InputStream content = new ByteArrayInputStream ("Hello, World!" .getBytes (UTF_8 ));
167+
168+ BlobId blobId = BlobId .of (bucketName , blobName );
169+ BlobInfo blobInfo = BlobInfo .newBuilder (blobId )
170+ .setContentType ("text/plain" )
171+ .build ();
172+ Blob blob = storage .create (blobInfo , content , BlobWriteOption .encryptionKey (encryptionKey ));
173+ // [END storageUploadEncryptedFile]
174+ return blob ;
175+ }
176+
156177 /**
157178 * Example of getting information on a bucket, only if its metageneration matches a value,
158179 * otherwise a {@link StorageException} is thrown.
@@ -470,7 +491,7 @@ public byte[] readBlobFromStringsWithGeneration(String bucketName, String blobNa
470491 // [TARGET readAllBytes(BlobId, BlobSourceOption...)]
471492 // [VARIABLE "my_unique_bucket"]
472493 // [VARIABLE "my_blob_name"]
473- // [VARIABLE 42" ]
494+ // [VARIABLE 42]
474495 public byte [] readBlobFromId (String bucketName , String blobName , long blobGeneration ) {
475496 // [START readBlobFromId]
476497 BlobId blobId = BlobId .of (bucketName , blobName , blobGeneration );
@@ -479,6 +500,21 @@ public byte[] readBlobFromId(String bucketName, String blobName, long blobGenera
479500 return content ;
480501 }
481502
503+ /**
504+ * Example of reading all bytes of an encrypted blob.
505+ */
506+ // [TARGET readAllBytes(BlobId, BlobSourceOption...)]
507+ // [VARIABLE "my_unique_bucket"]
508+ // [VARIABLE "my_blob_name"]
509+ // [VARIABLE "my_encryption_key"]
510+ public byte [] readEncryptedBlob (String bucketName , String blobName , String decryptionKey ) {
511+ // [START readEncryptedBlob]
512+ byte [] content = storage .readAllBytes (
513+ bucketName , blobName , BlobSourceOption .decryptionKey (decryptionKey ));
514+ // [END readEncryptedBlob]
515+ return content ;
516+ }
517+
482518 /**
483519 * Example of using a batch request to delete, update and get a blob.
484520 */
0 commit comments