code: public Bucket lockRetentionPolicy(String bucketName) throws StorageException {
      // [START storage_lock_retention_policy]
      // Instantiate a Google Cloud Storage client
      Storage storage = StorageOptions.getDefaultInstance().getService();
  
      // The name of a bucket, e.g. "my-bucket"
      // String bucketName = "my-bucket";
  
      Bucket bucket =
          storage.get(bucketName, Storage.BucketGetOption.fields(BucketField.METAGENERATION));
      Bucket lockedBucket =
          bucket.lockRetentionPolicy(Storage.BucketTargetOption.metagenerationMatch());
  
      System.out.println("Retention period for " + bucketName + " is now locked");
      System.out.println(
          "Retention policy effective as of " + new Date(lockedBucket.getRetentionEffectiveTime()));
      // [END storage_lock_retention_policy]
      return lockedBucket;
    }
docs: Example of how to lock a bucket retention policy