Skip to content

Commit 15d9861

Browse files
authored
GCS w/ KMS Samples (#3323)
* Add samples for 'storage_upload_with_kms_key' and 'storage_set_bucket_default_kms_key'. * Remove accidental newline. * Remove use of deprecated infoStream create. * Address feedback. * Fix test method name. * Additional feedback. * Fix testBlobAcl test. * AssertEquals instead of assertTrue. * Update formating.
1 parent f4adabf commit 15d9861

2 files changed

Lines changed: 76 additions & 7 deletions

File tree

google-cloud-examples/src/main/java/com/google/cloud/examples/storage/snippets/StorageSnippets.java

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,17 +168,41 @@ public Blob createBlobFromInputStream(String bucketName, String blobName) {
168168
// [VARIABLE "my_encryption_key"]
169169
public Blob createEncryptedBlob(String bucketName, String blobName, String encryptionKey) {
170170
// [START storageUploadEncryptedFile]
171-
InputStream content = new ByteArrayInputStream("Hello, World!".getBytes(UTF_8));
171+
byte[] data = "Hello, World!".getBytes(UTF_8);
172172

173173
BlobId blobId = BlobId.of(bucketName, blobName);
174174
BlobInfo blobInfo = BlobInfo.newBuilder(blobId)
175175
.setContentType("text/plain")
176176
.build();
177-
Blob blob = storage.create(blobInfo, content, BlobWriteOption.encryptionKey(encryptionKey));
177+
Blob blob = storage.create(blobInfo, data, BlobTargetOption.encryptionKey(encryptionKey));
178178
// [END storageUploadEncryptedFile]
179179
return blob;
180180
}
181181

182+
/**
183+
* Example of uploading a blob encrypted service side with a Cloud KMS key.
184+
*/
185+
public Blob createKmsEncrpytedBlob(String bucketName, String blobName, String kmsKeyName) {
186+
// [START storage_upload_with_kms_key]
187+
byte[] data = "Hello, World!".getBytes(UTF_8);
188+
189+
// The name of the existing bucket to set a default KMS key for, e.g. "my-bucket"
190+
// String bucketName = "my-bucket"
191+
192+
// The name of the KMS-key to use as a default
193+
// Key names are provided in the following format:
194+
// 'projects/<PROJECT>/locations/<LOCATION>/keyRings/<RING_NAME>/cryptoKeys/<KEY_NAME>'
195+
// String kmsKeyName = ""
196+
197+
BlobId blobId = BlobId.of(bucketName, blobName);
198+
BlobInfo blobInfo = BlobInfo.newBuilder(blobId)
199+
.setContentType("text/plain")
200+
.build();
201+
Blob blob = storage.create(blobInfo, data, BlobTargetOption.kmsKeyName(kmsKeyName));
202+
// [END storage_upload_with_kms_key]
203+
return blob;
204+
}
205+
182206
/**
183207
* Example of getting information on a bucket, only if its metageneration matches a value,
184208
* otherwise a {@link StorageException} is thrown.
@@ -1137,4 +1161,31 @@ public void downloadFileUsingRequesterPays(String projectId, String bucketName,
11371161
blob.downloadTo(destFilePath, Blob.BlobSourceOption.userProject(projectId));
11381162
// [END storage_download_file_requester_pays]
11391163
}
1164+
1165+
/**
1166+
* Example of setting a default KMS key on a bucket.
1167+
*/
1168+
public Bucket setDefaultKmsKey(String bucketName, String kmsKeyName) throws StorageException {
1169+
// [START storage_set_bucket_default_kms_key]
1170+
// Instantiate a Google Cloud Storage client
1171+
Storage storage = StorageOptions.getDefaultInstance().getService();
1172+
1173+
// The name of the existing bucket to set a default KMS key for, e.g. "my-bucket"
1174+
// String bucketName = "my-bucket"
1175+
1176+
// The name of the KMS-key to use as a default
1177+
// Key names are provided in the following format:
1178+
// 'projects/<PROJECT>/locations/<LOCATION>/keyRings/<RING_NAME>/cryptoKeys/<KEY_NAME>'
1179+
// String kmsKeyName = ""
1180+
1181+
BucketInfo bucketInfo = BucketInfo.newBuilder(bucketName)
1182+
.setDefaultKmsKeyName(kmsKeyName)
1183+
.build();
1184+
1185+
Bucket bucket = storage.update(bucketInfo);
1186+
1187+
System.out.println("Default KMS Key Name: " + bucket.getDefaultKmsKeyName());
1188+
// [END storage_set_bucket_default_kms_key]
1189+
return bucket;
1190+
}
11401191
}

google-cloud-examples/src/test/java/com/google/cloud/examples/storage/snippets/ITStorageSnippets.java

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ public class ITStorageSnippets {
7272
private static final String USER_EMAIL = "google-cloud-java-tests@"
7373
+ "java-docs-samples-tests.iam.gserviceaccount.com";
7474

75+
private static final String KMS_KEY_NAME = "projects/gcloud-devel/locations/us/"
76+
+ "keyRings/gcs_kms_key_ring_us/cryptoKeys/key";
77+
7578
private static Storage storage;
7679
private static StorageSnippets storageSnippets;
7780
private static List<String> bucketsToCleanUp;
@@ -178,6 +181,13 @@ public void testCreateUpdateEncryptedBlob() throws InterruptedException {
178181
assertEquals("text/plain", blob.getContentType());
179182
}
180183

184+
@Test
185+
public void testCreateKMSEncryptedBlob() {
186+
String blobName = "kms-encrypted-blob";
187+
Blob blob = storageSnippets.createKmsEncrpytedBlob(BUCKET, blobName, KMS_KEY_NAME);
188+
assertNotNull(blob);
189+
}
190+
181191
@Test
182192
public void testCreateCopyAndGetBlob() {
183193
String blobName = "test-create-copy-get-blob";
@@ -383,22 +393,22 @@ public void testBlobAcl() {
383393
assertNull(storageSnippets.getBlobAcl(BUCKET, blobName, createdBlob.getGeneration()));
384394
// test non-existing blob
385395
String nonExistingBlob = "test-blob-acl";
386-
assertNull(storageSnippets.getBlobAcl(BUCKET, nonExistingBlob, -1L));
387-
assertFalse(storageSnippets.deleteBlobAcl(BUCKET, nonExistingBlob, -1L));
396+
assertNull(storageSnippets.getBlobAcl(BUCKET, nonExistingBlob, 1L));
397+
assertFalse(storageSnippets.deleteBlobAcl(BUCKET, nonExistingBlob, 1L));
388398
try {
389-
storageSnippets.createBlobAcl(BUCKET, nonExistingBlob, -1L);
399+
storageSnippets.createBlobAcl(BUCKET, nonExistingBlob, 1L);
390400
fail("Expected StorageException");
391401
} catch (StorageException ex) {
392402
// expected
393403
}
394404
try {
395-
storageSnippets.updateBlobAcl(BUCKET, nonExistingBlob, -1L);
405+
storageSnippets.updateBlobAcl(BUCKET, nonExistingBlob, 1L);
396406
fail("Expected StorageException");
397407
} catch (StorageException ex) {
398408
// expected
399409
}
400410
try {
401-
storageSnippets.listBlobAcls(BUCKET, nonExistingBlob, -1L);
411+
storageSnippets.listBlobAcls(BUCKET, nonExistingBlob, 1L);
402412
fail("Expected StorageException");
403413
} catch (StorageException ex) {
404414
// expected
@@ -429,4 +439,12 @@ public void testRequesterPays() throws Exception {
429439
bucket = storageSnippets.disableRequesterPays(BUCKET);
430440
assertFalse(bucket.requesterPays());
431441
}
442+
443+
@Test
444+
public void testDefaultKMSKey(){
445+
Bucket bucket = storageSnippets.setDefaultKmsKey(BUCKET, KMS_KEY_NAME);
446+
assertEquals(KMS_KEY_NAME, bucket.getDefaultKmsKeyName());
447+
// Remove default key
448+
storageSnippets.setDefaultKmsKey(BUCKET,null);
449+
}
432450
}

0 commit comments

Comments
 (0)