Java Code Examples for com.google.cloud.storage.Bucket#retentionPolicyIsLocked()

The following examples show how to use com.google.cloud.storage.Bucket#retentionPolicyIsLocked() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: StorageSnippets.java    From google-cloud-java with Apache License 2.0 6 votes vote down vote up
/** Example of removing a retention policy on a bucket */
public Bucket removeRetentionPolicy(String bucketName)
    throws StorageException, IllegalArgumentException {
  // [START storage_remove_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, BucketGetOption.fields(BucketField.RETENTION_POLICY));
  if (bucket.retentionPolicyIsLocked() != null && bucket.retentionPolicyIsLocked()) {
    throw new IllegalArgumentException(
        "Unable to remove retention period as retention policy is locked.");
  }

  Bucket bucketWithoutRetentionPolicy =
      bucket.toBuilder().setRetentionPeriod(null).build().update();

  System.out.println("Retention period for " + bucketName + " has been removed");
  // [END storage_remove_retention_policy]
  return bucketWithoutRetentionPolicy;
}
 
Example 2
Source File: StorageSnippets.java    From google-cloud-java with Apache License 2.0 6 votes vote down vote up
/** Example of how to get a bucket's retention policy */
public Bucket getRetentionPolicy(String bucketName) throws StorageException {
  // [START storage_get_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, BucketGetOption.fields(BucketField.RETENTION_POLICY));

  System.out.println("Retention Policy for " + bucketName);
  System.out.println("Retention Period: " + bucket.getRetentionPeriod());
  if (bucket.retentionPolicyIsLocked() != null && bucket.retentionPolicyIsLocked()) {
    System.out.println("Retention Policy is locked");
  }
  if (bucket.getRetentionEffectiveTime() != null) {
    System.out.println("Effective Time: " + new Date(bucket.getRetentionEffectiveTime()));
  }
  // [END storage_get_retention_policy]
  return bucket;
}