Skip to content

Commit 8e389fb

Browse files
committed
Add samples for 'storage_upload_with_kms_key' and 'storage_set_bucket_default_kms_key'.
1 parent cd9d226 commit 8e389fb

2 files changed

Lines changed: 67 additions & 0 deletions

File tree

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

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,27 @@ public Blob createEncryptedBlob(String bucketName, String blobName, String encry
179179
return blob;
180180
}
181181

182+
/**
183+
* Example of uploading a blob encrypted by a KMS-key.
184+
*/
185+
// [TARGET create(BlobInfo, InputStream, BlobWriteOption...)]
186+
// [VARIABLE "my_unique_bucket"]
187+
// [VARIABLE "my_blob_name"]
188+
// [VARIABLE "kms_key_name"]
189+
public Blob createKmsEncrpytedBlob(String bucketName, String blobName, String kmsKeyName) {
190+
// [START storage_upload_with_kms_key]
191+
InputStream content = new ByteArrayInputStream("Hello, World!".getBytes(UTF_8));
192+
193+
BlobId blobId = BlobId.of(bucketName, blobName);
194+
BlobInfo blobInfo = BlobInfo.newBuilder(blobId)
195+
.setContentType("text/plain")
196+
.build();
197+
Blob blob = storage.create(blobInfo, content, BlobWriteOption.kmsKeyName(kmsKeyName));
198+
// [END storage_upload_with_kms_key]
199+
200+
return blob;
201+
}
202+
182203
/**
183204
* Example of getting information on a bucket, only if its metageneration matches a value,
184205
* otherwise a {@link StorageException} is thrown.
@@ -1133,8 +1154,34 @@ public void downloadFileUsingRequesterPays(String projectId, String bucketName,
11331154
// Get specific file from specified bucket
11341155
Blob blob = storage.get(BlobId.of(bucketName, srcFilename));
11351156

1157+
11361158
// Download file to specified path
11371159
blob.downloadTo(destFilePath, Blob.BlobSourceOption.userProject(projectId));
11381160
// [END storage_download_file_requester_pays]
11391161
}
1162+
1163+
1164+
/**
1165+
* Example of retrieving Requester pays status on a bucket.
1166+
*/
1167+
public Bucket setDefaultKMSKey(String bucketName, String kmsKeyName) throws StorageException {
1168+
// [START storage_set_bucket_default_kms_key]
1169+
// Instantiate a Google Cloud Storage client
1170+
Storage storage = StorageOptions.getDefaultInstance().getService();
1171+
1172+
// The name of the KMS-key to use as a default
1173+
// Key names are provided in the following format:
1174+
// 'projects/<PROJECT>/locations/<LOCATION>/keyRings/<RING_NAME>/cryptoKeys/<KEY_NAME>'
1175+
// String kmsKeyname = ""
1176+
1177+
BucketInfo bucketInfo = BucketInfo.newBuilder(bucketName)
1178+
.setDefaultKmsKeyName(kmsKeyName)
1179+
.build();
1180+
1181+
Bucket bucket = storage.update(bucketInfo);
1182+
1183+
System.out.println("Default KMS Key Name: " + bucket.getDefaultKmsKeyName());
1184+
// [END storage_set_bucket_default_kms_key]
1185+
return bucket;
1186+
}
11401187
}

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ 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 = ""; //TODO: Supply KMS_KEY_NAME?
76+
7577
private static Storage storage;
7678
private static StorageSnippets storageSnippets;
7779
private static List<String> bucketsToCleanUp;
@@ -178,6 +180,16 @@ public void testCreateUpdateEncryptedBlob() throws InterruptedException {
178180
assertEquals("text/plain", blob.getContentType());
179181
}
180182

183+
@Test
184+
public void testCreateKMSEncryptedBlob() {
185+
String kmsKeyName = "";
186+
String blobName = "kms-encrypted-blob";
187+
188+
Blob blob = storageSnippets.createKmsEncrpytedBlob(BUCKET, blobName, kmsKeyName);
189+
190+
assertNotNull(blob);
191+
}
192+
181193
@Test
182194
public void testCreateCopyAndGetBlob() {
183195
String blobName = "test-create-copy-get-blob";
@@ -429,4 +441,12 @@ public void testRequesterPays() throws Exception {
429441
bucket = storageSnippets.disableRequesterPays(BUCKET);
430442
assertFalse(bucket.requesterPays());
431443
}
444+
445+
@Test
446+
public void testDefaultKMSKey(){
447+
Bucket bucket = storageSnippets.setDefaultKMSKey(BUCKET, KMS_KEY_NAME);
448+
assertTrue(KMS_KEY_NAME.equals(bucket.getDefaultKmsKeyName()));
449+
// Remove default key
450+
storageSnippets.setDefaultKMSKey(BUCKET,"");
451+
}
432452
}

0 commit comments

Comments
 (0)