Hands-on Object Lifecycle Management on GCP with CloudShell


In GCP, we always store our objects on Cloud Storage. However, if we keep all the things in standard storage, it will be expensive. Therefore, this article will teach you how to set lifecycle through CloudShell step by step. We can add our own lifecycle policy to control Cloud Storage. It automatically moves objects to different storage classes based on some attributes.


In console, we choose Cloud Shell and then click the editor mode such that we could easily review our code.

The we have to setup our environment e.g. define project, zone and region:

gcloud config set project <you-project-id>
gcloud config set compute/zone us-central1-a
gcloud config set compute/region us-central1

To download our prepared file first:

git clone https://github.com/manbobo2002/cloud-storage-migration.git
cd ./cloud-storage-migration

Then in our root directory, we run:

sh setup.sh

This setup script will do the following things:

  1. create bucket
  2. make bucket to public
  3. copy test.txt into the bucket
  4. make test.txt to public
  5. try to access test.txt file using CURL

Edit Lifecycle Policies

Now, we are going to run:

sh test.sh

Then we can easily apply our lifecycle policy into our bucket.

In this example, we directly move all the storage with standard class to nearline class. Also, if objects were over 1095 days, then it will move to the coldline class.

If we are not sure whether the policies are applied or not, we can directly go to Cloud Storage and see the lifecycle tab, we should see there are two rules here. Note that normally it will take around 1 day to take effect.


To clean all the things, we just have to run:

sh cleanup.sh

This will delete our bucket we just created.

Leave a Reply