Working with GCS Bucket Lock
- Define an environment variable named Cloud Storage_BUCKET and use your Project ID as the bucket name. Use the following command which uses the Cloud SDK to get your Project ID.
export BUCKET="$(gcloud config get-value project)"
- Next, make a new bucket using the following gsutil command:
gsutil mb "gs://$BUCKET"
- You can define the Retention Policy using seconds, days, months, and years using the Cloud Storage gsutil tool. As an example, in Cloud Shell, create a Retention Policy for 10 seconds.
gsutil retention set 10s "gs://$BUCKET"
- Verify the Retention Policy for a bucket:
gsutil retention get "gs://$BUCKET"
- Now that the bucket has a Retention Policy, add a dummy transaction records object to test it:
gsutil cp gs://cloud-samples-data/storage/bucket-lock/dummy_transactions "gs://$BUCKET/"
- Review the retention expiration:
gsutil ls -L "gs://$BUCKET/dummy_transactions"
- Lock the Retention Policy:
gsutil retention lock "gs://$BUCKET/"
- Set a temporary hold on the dummy transactions object:
gsutil retention temp set "gs://$BUCKET/dummy_transactions"
- By placing a temporary hold on the object, delete operations are not possible unless the object is released from the hold. As an example, attempt to delete the object:
gsutil rm "gs://$BUCKET/dummy_transactions"
- Once regulators conclude their audit, the Branch IT Administrator removes the temporary hold. Use the following command to release the hold:
gsutil retention temp release "gs://$BUCKET/dummy_transactions"
- Enable the default event-based hold for your bucket using the following command:
gsutil retention event-default set "gs://$BUCKET/"
- Add a dummy loan into the bucket using the following command:
gsutil cp gs://cloud-samples-data/storage/bucket-lock/dummy_loan "gs://$BUCKET/"
- Verify that the event-based hold is enabled for your newly added dummy loan using the following command:
gsutil ls -L "gs://$BUCKET/dummy_loan"
- When the loan is paid off, the Branch IT Administrator then releases the event-based hold using the following command:
gsutil retention event release "gs://$BUCKET/dummy_loan"
- After an event-based hold is released, the bucket Retention Policy takes effect. Verify that the dummy loan now has a Retention Expiration field using the following command:
gsutil ls -L "gs://$BUCKET/dummy_loan"
- Delete an empty bucket using the following command:
gsutil rb "gs://$BUCKET/"
Tag:Google Cloud