code: public Bucket enableBucketPolicyOnly(String bucketName) throws StorageException {
      // [START storage_enable_bucket_policy_only]
      // Instantiate a Google Cloud Storage client
      Storage storage = StorageOptions.getDefaultInstance().getService();
  
      // The name of a bucket, e.g. "my-bucket"
      // String bucketName = "my-bucket";
  
      BucketInfo.IamConfiguration iamConfiguration =
          BucketInfo.IamConfiguration.newBuilder().setIsBucketPolicyOnlyEnabled(true).build();
      Bucket bucket =
          storage.update(
              BucketInfo.newBuilder(bucketName).setIamConfiguration(iamConfiguration).build());
  
      System.out.println("Bucket Policy Only was enabled for " + bucketName);
      // [END storage_enable_bucket_policy_only]
      return bucket;
    }
docs: Example of how to enable Bucket Policy Only for a bucket