In this post I’ll cover how to not only create a GCS bucket via gcloud but more importantly, how to assign lifecycle policies to the created bucket.
You’ll need a GCP account, gcloud, and adequate GCP IAM permissions.
Let’s get started!
Update — Autoclass option:
GCP has added the Autoclass feature for automatically changing storage class “based on each objects access patterns”. According to the documentation this feature must be set at bucket creation for it to work.
Creating the bucket:
PROJECT="my-project"
BUCKET="my-bucket"
gcloud storage buckets create gs://$BUCKET --project=$PROJECT \
--default-storage-class=standard --placement=us-east4,us-west1
Setting Lifecycle Policies:
You can update buckets after they are created. Here is a link to the GCP documentation that covers options for updating buckets.
In our example we’re going to update the bucket’s lifecycle polices. We want to set the policies for the bucket as follows:
- files older than 30 days are set to ‘NEARLINE’
- files older than 90 days are set to ‘COLDLINE’
- files older than 365 days are set to ‘ARCHIVE’
These rules can be turned into the following JSON schema:
{
"rule": [
{
"action": {
"storageClass": "NEARLINE",
"type": "SetStorageClass"
},
"condition": {
"age": 30
}
},
{
"action": {
"storageClass": "COLDLINE",
"type": "SetStorageClass"
},
"condition": {
"age": 90
}
},
{
"action": {
"storageClass": "ARCHIVE",
"type": "SetStorageClass"
},
"condition": {
"age": 365
}
}
]
}
Save the above schema as a file such as lifecycle-file.json and run the following command:
PROJECT="my-project"
BUCKET="my-bucket"
gcloud storage buckets update gs://$BUCKET --project=$PROJECT \
--lifecycle-file=./lifecycle-file.json
The rules have now been applied to your bucket!