public Bucket setDefaultKmsKey(String bucketName, String kmsKeyName) throws StorageException {
    // [START storage_set_bucket_default_kms_key]
    // Instantiate a Google Cloud Storage client
    Storage storage = StorageOptions.getDefaultInstance().getService();

    // The name of the existing bucket to set a default KMS key for, e.g. "my-bucket"
    // String bucketName = "my-bucket"

    // The name of the KMS-key to use as a default
    // Key names are provided in the following format:
    // 'projects/<PROJECT>/locations/<LOCATION>/keyRings/<RING_NAME>/cryptoKeys/<KEY_NAME>'
    // String kmsKeyName = ""

    BucketInfo bucketInfo =
        BucketInfo.newBuilder(bucketName).setDefaultKmsKeyName(kmsKeyName).build();

    Bucket bucket = storage.update(bucketInfo);

    System.out.println("Default KMS Key Name: " + bucket.getDefaultKmsKeyName());
    // [END storage_set_bucket_default_kms_key]
    return bucket;
  }
public Bucket setDefaultKmsKey ( String bucketName , String kmsKeyName ) throws StorageException { Storage storage = StorageOptions . getDefaultInstance ( ) . getService ( ) ; BucketInfo bucketInfo = BucketInfo . newBuilder ( bucketName ) . setDefaultKmsKeyName ( kmsKeyName ) . build ( ) ; Bucket bucket = storage . update ( bucketInfo ) ; System . out . println ( "Default KMS Key Name: " + bucket . getDefaultKmsKeyName ( ) ) ; return bucket ; }
Example of setting a default KMS key on a bucket.