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