@@ -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}
0 commit comments