public Bucket setRetentionPolicy(String bucketName, Long retentionPeriod)
      throws StorageException {
    // [START storage_set_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";

    // The retention period for objects in bucket
    // Long retentionPeriod = 3600L; // 1 hour in seconds

    Bucket bucketWithRetentionPolicy =
        storage.update(
            BucketInfo.newBuilder(bucketName).setRetentionPeriod(retentionPeriod).build());

    System.out.println(
        "Retention period for "
            + bucketName
            + " is now "
            + bucketWithRetentionPolicy.getRetentionPeriod());
    // [END storage_set_retention_policy]
    return bucketWithRetentionPolicy;
  }
public Bucket setRetentionPolicy ( String bucketName , Long retentionPeriod ) throws StorageException { Storage storage = StorageOptions . getDefaultInstance ( ) . getService ( ) ; Bucket bucketWithRetentionPolicy = storage . update ( BucketInfo . newBuilder ( bucketName ) . setRetentionPeriod ( retentionPeriod ) . build ( ) ) ; System . out . println ( "Retention period for " + bucketName + " is now " + bucketWithRetentionPolicy . getRetentionPeriod ( ) ) ; return bucketWithRetentionPolicy ; }
Example of setting a retention policy on a bucket