Skip to content

Commit 82cdfb0

Browse files
Jerjou Chenggarrettjonesgoogle
authored andcommitted
---
yaml --- r: 7701 b: refs/heads/tswast-patch-1 c: 37ca986 h: refs/heads/master i: 7699: f0eda6b
1 parent 01acbed commit 82cdfb0

4 files changed

Lines changed: 76 additions & 3 deletions

File tree

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,5 @@ refs/tags/v0.18.0: 9d193c4c4b9d1c6f21515dd8e50836b9194ec9bb
5757
refs/tags/v0.19.0: e67b56e4d8dad5f9a7b38c9b2107c23c828f2ed5
5858
refs/tags/v0.20.0: 839f7fb7156535146aa1cb2c5aadd8d375d854e8
5959
refs/tags/v0.20.1: 370471f437f1f4f68a11e068df5cd6bf39edb1fa
60-
refs/heads/tswast-patch-1: 5dfb90466366370a0e0bfaeea476e4cfdb29b209
60+
refs/heads/tswast-patch-1: 37ca9863b9f3a9dc0aec9b3a41c490790cacb92f
6161
refs/heads/pubsub-streaming-pull: 19262b752ee874eb2ca3b950eb2aef44d5a5267b

branches/tswast-patch-1/google-cloud-examples/src/main/java/com/google/cloud/examples/storage/snippets/StorageSnippets.java

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import com.google.cloud.storage.Storage.BlobListOption;
4545
import com.google.cloud.storage.Storage.BlobSourceOption;
4646
import com.google.cloud.storage.Storage.BlobTargetOption;
47+
import com.google.cloud.storage.Storage.BlobWriteOption;
4748
import com.google.cloud.storage.Storage.BucketGetOption;
4849
import com.google.cloud.storage.Storage.BucketListOption;
4950
import 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
*/

branches/tswast-patch-1/google-cloud-examples/src/test/java/com/google/cloud/examples/storage/snippets/ITStorageSnippets.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,20 @@ public void testBlob() throws InterruptedException {
145145
copiedBlob.delete();
146146
}
147147

148+
@Test
149+
public void testCreateEncryptedBlob() throws InterruptedException {
150+
// Note: DO NOT put your encryption key in your code, like it is here. Store it somewhere safe,
151+
// and read it in when you need it. This key is just here to make the code easier to read.
152+
String encryptionKey = "0mMWhFvQOdS4AmxRpo8SJxXn5MjFhbz7DkKBUdUIef8=";
153+
154+
String blobName = "encrypted-blob";
155+
Blob blob = storageSnippets.createEncryptedBlob(BUCKET, blobName, encryptionKey);
156+
157+
assertNotNull(blob);
158+
byte[] encryptedContent = storageSnippets.readEncryptedBlob(BUCKET, blobName, encryptionKey);
159+
assertEquals("Hello, World!", new String(encryptedContent));
160+
}
161+
148162
@Test
149163
public void testCreateCopyAndGetBlob() {
150164
String blobName = "test-create-copy-get-blob";

branches/tswast-patch-1/google-cloud-storage/src/main/java/com/google/cloud/storage/Storage.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1566,6 +1566,20 @@ public static Builder newBuilder() {
15661566
* Blob blob = storage.create(blobInfo, content);
15671567
* }</pre>
15681568
*
1569+
* <p>Example of uploading an encrypted blob.
1570+
* <pre> {@code
1571+
* String bucketName = "my_unique_bucket";
1572+
* String blobName = "my_blob_name";
1573+
* String encryptionKey = "my_encryption_key";
1574+
* InputStream content = new ByteArrayInputStream("Hello, World!".getBytes(UTF_8));
1575+
*
1576+
* BlobId blobId = BlobId.of(bucketName, blobName);
1577+
* BlobInfo blobInfo = BlobInfo.newBuilder(blobId)
1578+
* .setContentType("text/plain")
1579+
* .build();
1580+
* Blob blob = storage.create(blobInfo, content, BlobWriteOption.encryptionKey(encryptionKey));
1581+
* }</pre>
1582+
*
15691583
* @return a [@code Blob} with complete information
15701584
* @throws StorageException upon failure
15711585
*/
@@ -1921,11 +1935,20 @@ public static Builder newBuilder() {
19211935
* <pre> {@code
19221936
* String bucketName = "my_unique_bucket";
19231937
* String blobName = "my_blob_name";
1924-
* long blobGeneration = 42";
1938+
* long blobGeneration = 42;
19251939
* BlobId blobId = BlobId.of(bucketName, blobName, blobGeneration);
19261940
* byte[] content = storage.readAllBytes(blobId);
19271941
* }</pre>
19281942
*
1943+
* <p>Example of reading all bytes of an encrypted blob.
1944+
* <pre> {@code
1945+
* String bucketName = "my_unique_bucket";
1946+
* String blobName = "my_blob_name";
1947+
* String decryptionKey = "my_encryption_key";
1948+
* byte[] content = storage.readAllBytes(
1949+
* bucketName, blobName, BlobSourceOption.decryptionKey(decryptionKey));
1950+
* }</pre>
1951+
*
19291952
* @return the blob's content
19301953
* @throws StorageException upon failure
19311954
*/

0 commit comments

Comments
 (0)