CareerByteCode
ByteBlogby CareerByteCode

Cloud Storage Deep Dive: Classes, Lifecycle and Signed URLs

What you build: a private bucket with storage classes, a lifecycle policy, object versions and expiring signed URLs, in your own Google Cloud account. Every step tested.

Cloud Storage Deep Dive: Classes, Lifecycle and Signed URLs

✍️ Translate this blog into Hindi, Tamil or Telugu

In this lab you go beyond "upload a file": you create a private bucket, move a file to a cheaper storage class, write a lifecycle policy that does that for you as files age, keep and restore old versions of a file, and share one private file with a signed URL that stops working after a few minutes. You finish by deleting everything.

Who it is for: beginners who have uploaded a file to the cloud before and want to understand how real teams store files safely and cheaply. Time: about 1 hour. Cost: free tier (Cloud Storage gives 5 GB of Standard storage a month in us-east1, us-west1 and us-central1 at no charge; this lab stores under 1 KB for a few minutes).

Every command below was run for real by the CareerByteCode Team on a fresh Google Cloud project, and every output and screenshot is the real result we got. Three things did not work the first time; all three are in the lab with the fix. We ran the commands from a terminal with the Google Cloud CLI (gcloud and curl); Cloud Shell in your browser has the same tools ready.

⭐ Recommended by CareerByteCode · BytePlay courseNVIDIA GPU AI Factory: Architecture, Engineering & Operationsโ‚น40,000Start learning →

Part A: Cloud Storage in plain English

Cloud Storage keeps files (called objects) in buckets. A bucket has a globally unique name and a location. You pay for what you store, how you read it, and the network.

The life of a file in Cloud Storage
Lifecycle: Standard, Nearline, Coldline, deleted; a signed URL shares one file for a short time

Six words you will use in this lab:

  • Storage class: the price plan of one object. Standard (cheapest to read, dearest to keep), Nearline (about once a month), Coldline (about once a quarter), Archive (about once a year). The colder the class, the lower the storage price and the higher the read fee and minimum stay (30, 90, 365 days).
  • Lifecycle policy: rules on the bucket like "after 30 days, move to Nearline" or "after 365 days, delete". Google runs them for you every day. Real-life example: a standing order to the office clerk.
  • Versioning: when you overwrite or delete a file, the old copy is kept as a noncurrent version. Real-life example: the "version history" of a shared document.
  • Uniform bucket-level access: permissions are set on the whole bucket with IAM only, not file by file. Simpler and safer.
  • Public access prevention: a lock that stops anyone from making the bucket or its files public by mistake.
  • Signed URL: a normal web link with a signature and an expiry time inside it. Whoever has the link can download that one file until it expires, without a Google account. Real-life example: a visitor pass that works only today.

Before you start

  1. A Google Cloud account (the free trial works) and a project with billing linked. Create one at https://console.cloud.google.com/projectcreate if you do not have one.
  2. Open Cloud Shell (the >_ icon at the top right of the Google Cloud console). It already has gcloud and curl.
  3. Save your project id in a variable. Replace the text in quotes with your own project id:
export PROJECT_ID="your-project-id"
  1. Bucket names are global. We use cbc-gcs-lab- plus your project id, which nobody else has. Each step sets BUCKET again, so you can run any step on its own.

Part B: the lab

Step 1: create a private bucket

Why: --location=us-east1 keeps the files in one region (inside the free tier). --uniform-bucket-level-access and --public-access-prevention make it private by design.

gcloud config set project $PROJECT_ID
export BUCKET=cbc-gcs-lab-$PROJECT_ID
gcloud storage buckets create gs://$BUCKET --location=us-east1 \
  --default-storage-class=STANDARD --uniform-bucket-level-access --public-access-prevention
gcloud storage buckets describe gs://$BUCKET --format="yaml(location,default_storage_class,uniform_bucket_level_access,public_access_prevention)"
Updated property [core/project].
Creating gs://cbc-gcs-lab-your-project-id/...
default_storage_class: STANDARD
location: US-EAST1
public_access_prevention: enforced
uniform_bucket_level_access: true

public_access_prevention: enforced means no file in this bucket can ever be made public, even by mistake.

Step 2: upload three files

Why: two small invoices and a brochure give us something to move, version and share. gcloud storage ls -l lists size, time and name.

export BUCKET=cbc-gcs-lab-$PROJECT_ID
echo "Invoice 1001: 3 notebooks" > invoice-1001.txt
echo "Invoice 1002: 1 backpack" > invoice-1002.txt
echo "Course brochure v1" > brochure.txt
gcloud storage cp invoice-1001.txt invoice-1002.txt brochure.txt gs://$BUCKET/
gcloud storage ls -l gs://$BUCKET/
Copying file://invoice-1001.txt to gs://cbc-gcs-lab-your-project-id/invoice-1001.txt
Copying file://invoice-1002.txt to gs://cbc-gcs-lab-your-project-id/invoice-1002.txt
  
Copying file://brochure.txt to gs://cbc-gcs-lab-your-project-id/brochure.txt
..........

Average throughput: 126.4B/s
        19  2026-11-18T06:36:43Z  gs://cbc-gcs-lab-your-project-id/brochure.txt
        26  2026-11-18T06:36:43Z  gs://cbc-gcs-lab-your-project-id/invoice-1001.txt
        25  2026-11-18T06:36:42Z  gs://cbc-gcs-lab-your-project-id/invoice-1002.txt
TOTAL: 3 objects, 70 bytes (70.00B)
Bucket and upload
Steps 1 and 2: the private bucket and the three files

Step 3: change the class of one object

Why: invoice 1001 is old and rarely read, so we move it to NEARLINE by hand. Google rewrites the object in the new class; the name and the link stay the same.

export BUCKET=cbc-gcs-lab-$PROJECT_ID
gcloud storage objects update gs://$BUCKET/invoice-1001.txt --storage-class=NEARLINE
gcloud storage objects list gs://$BUCKET --format="table(name, storage_class, size)"
  
Rewriting gs://cbc-gcs-lab-your-project-id/invoice-1001.txt...
..........
NAME              STORAGE_CLASS  SIZE
brochure.txt      STANDARD       19
invoice-1001.txt  NEARLINE       26
invoice-1002.txt  STANDARD       25
Storage class
Step 3: invoice-1001.txt is now NEARLINE, the others stay STANDARD

Step 4: a lifecycle policy that does it for you

Why: nobody wants to move files by hand. This policy has four rules:

  1. A Standard object older than 30 days moves to Nearline.
  2. A Nearline object older than 90 days moves to Coldline.
  3. Any object older than 365 days is deleted.
  4. An old version is deleted once 2 newer versions exist (this keeps versioning from growing for ever).
export BUCKET=cbc-gcs-lab-$PROJECT_ID
cat > lifecycle.json <<EOF
{
  "rule": [
    {"action": {"type": "SetStorageClass", "storageClass": "NEARLINE"},
     "condition": {"age": 30, "matchesStorageClass": ["STANDARD"]}},
    {"action": {"type": "SetStorageClass", "storageClass": "COLDLINE"},
     "condition": {"age": 90, "matchesStorageClass": ["NEARLINE"]}},
    {"action": {"type": "Delete"}, "condition": {"age": 365}},
    {"action": {"type": "Delete"}, "condition": {"isLive": false, "numNewerVersions": 2}}
  ]
}
EOF
gcloud storage buckets update gs://$BUCKET --lifecycle-file=lifecycle.json
gcloud storage buckets describe gs://$BUCKET --format="json(lifecycle_config)"

The policy as Google saved it (end of the output):

  
Updating gs://cbc-gcs-lab-your-project-id/...
..........
{
  "lifecycle_config": {
    "rule": [
      {
        "action": {
          "storageClass": "NEARLINE",
          "type": "SetStorageClass"
        },
        "condition": {
          "age": 30,
...
          "storageClass": "COLDLINE",
          "type": "SetStorageClass"
        },
        "condition": {
          "age": 90,
          "matchesStorageClass": [
            "NEARLINE"
          ]
        }
      },
      {
        "action": {
          "type": "Delete"
        },
        "condition": {
          "age": 365
        }
      },
      {
        "action": {
          "type": "Delete"
        },
        "condition": {
          "isLive": false,
          "numNewerVersions": 2
        }
      }
    ]
  }
}
Lifecycle policy
Step 4: the four lifecycle rules saved on the bucket

Google checks the rules about once a day, so you will not see a file move during this lab. Our files are minutes old; the policy starts moving them after 30 days.

Step 5: versioning, keep old copies

Why: someone uploads a new brochure with wrong prices. With versioning on, the old brochure is not lost. --all-versions lists every copy; the number after # is the version id (called a generation).

export BUCKET=cbc-gcs-lab-$PROJECT_ID
gcloud storage buckets update gs://$BUCKET --versioning
echo "Course brochure v2 (new prices)" > brochure.txt
gcloud storage cp brochure.txt gs://$BUCKET/brochure.txt
gcloud storage ls --all-versions gs://$BUCKET/brochure.txt
echo "--- live copy:"; gcloud storage cat gs://$BUCKET/brochure.txt
  
Updating gs://cbc-gcs-lab-your-project-id/...
..........
Copying file://brochure.txt to gs://cbc-gcs-lab-your-project-id/brochure.txt
  
..........
gs://cbc-gcs-lab-your-project-id/brochure.txt#1794983803131282
gs://cbc-gcs-lab-your-project-id/brochure.txt#1794983858965956
--- live copy:
Course brochure v2 (new prices)

Step 6: restore the old version

Why: we copy the old generation on top of the live name. That brings v1 back and keeps v2 as history too.

export BUCKET=cbc-gcs-lab-$PROJECT_ID
OLD=$(gcloud storage ls --all-versions gs://$BUCKET/brochure.txt | head -1)
echo "restoring: ${OLD##*/}"
gcloud storage cp "$OLD" gs://$BUCKET/brochure.txt
echo "--- live copy now:"; gcloud storage cat gs://$BUCKET/brochure.txt
echo "--- versions kept: $(gcloud storage ls --all-versions gs://$BUCKET/brochure.txt | wc -l)"
restoring: brochure.txt#1794983803131282
Copying gs://cbc-gcs-lab-your-project-id/brochure.txt#1794983803131282 to gs://cbc-gcs-lab-your-project-id/brochure.txt
  
..........
--- live copy now:
Course brochure v1
--- versions kept: 3
Versions
Steps 5 and 6: two generations, then v1 restored; 3 versions kept

The live copy says "Course brochure v1" again, and 3 versions are kept: the first v1, v2 and the restored v1. Rule 4 of the lifecycle policy will delete the oldest one for you.

Why: prove the bucket is private. We ask for the file with a plain web address and no sign-in.

export BUCKET=cbc-gcs-lab-$PROJECT_ID
curl -s -o /dev/null -w "HTTP %{http_code}\n" https://storage.googleapis.com/$BUCKET/invoice-1002.txt
HTTP 403

HTTP 403 means "forbidden". Good: nobody can read your invoices by guessing the address.

Step 8: a service account that may only read this bucket

Why: a signed URL is signed by an identity. We create a small service account, url-signer, give it read-only access to this one bucket, and allow you to sign as it (the Service Account Token Creator role). No key file is created: Google signs for you.

A new account and a new role take a short while to reach every Google service, so this step has two until loops. The first one retries the bucket grant every 5 seconds: in one of our runs the bucket said the brand-new account "does not exist" for a few seconds, the grant failed silently and the signed link later got HTTP 403. The second loop tries every 10 seconds until you can act as url-signer; that took about a minute for us, and a fixed 60-second wait was not enough.

export BUCKET=cbc-gcs-lab-$PROJECT_ID
gcloud iam service-accounts create url-signer --display-name="Signs download links"
SA=url-signer@$PROJECT_ID.iam.gserviceaccount.com
until gcloud storage buckets add-iam-policy-binding gs://$BUCKET --member="serviceAccount:$SA" --role="roles/storage.objectViewer" > /dev/null 2>&1; do sleep 5; done
echo "objectViewer on the bucket: granted"
gcloud iam service-accounts add-iam-policy-binding $SA --member="user:$(gcloud config get-value account)" --role="roles/iam.serviceAccountTokenCreator" --format="value(etag)" > /dev/null && echo "you may sign as url-signer: granted"
until gcloud auth print-access-token --impersonate-service-account=$SA > /dev/null 2>&1; do sleep 10; done
echo "the new role has spread: you can act as url-signer now"
🧪⭐ Recommended by CareerByteCode · ByteLabs project bundleCloud and DevOps Realtime Starterโ‚น499Build the projects →
Created service account [url-signer].
Service account email: url-signer@your-project-id.iam.gserviceaccount.com
objectViewer on the bucket: granted
Updated IAM policy for serviceAccount [url-signer@your-project-id.iam.gserviceaccount.com].
you may sign as url-signer: granted
the new role has spread: you can act as url-signer now
Private bucket and signer
Steps 7 and 8: a plain link gets HTTP 403; the url-signer account is ready

POC: a signed URL downloads the private file

Sign a link for invoice 1002 that works for 10 minutes, then download it with curl like any visitor would. --region=us-east1 tells gcloud where the bucket is: url-signer may read files but not the bucket's settings, so without it the command stops with "Failed to auto-detect the region" (we met that on our first run).

export BUCKET=cbc-gcs-lab-$PROJECT_ID
gcloud storage sign-url gs://$BUCKET/invoice-1002.txt --duration=10m --region=us-east1 \
  --impersonate-service-account=url-signer@$PROJECT_ID.iam.gserviceaccount.com \
  --format="value(signed_url)" > url.txt
cut -c1-110 url.txt; echo "... (the link is $(wc -c < url.txt) characters long)"
curl -s -w "HTTP %{http_code}\n" "$(cat url.txt)"
https://cbc-gcs-lab-your-project-id.storage.googleapis.com/invoice-1002.txt?x-goog-signature=1afdc29f53811e
... (the link is 840 characters long)
Invoice 1002: 1 backpack
HTTP 200
Signed URL
POC: the 840-character signed link downloads the private invoice (HTTP 200)

Proof: the same file that gave HTTP 403 in Step 7 now downloads with HTTP 200, through a link anyone can open for 10 minutes, while the bucket stays private. The link holds the account name, the expiry and a signature; change one character and it stops working.

Break it and fix it

Links expire. We sign a link for 1 minute, use it at once, then wait 75 seconds and use it again.

export BUCKET=cbc-gcs-lab-$PROJECT_ID
gcloud storage sign-url gs://$BUCKET/invoice-1002.txt --duration=1m --region=us-east1 \
  --impersonate-service-account=url-signer@$PROJECT_ID.iam.gserviceaccount.com \
  --format="value(signed_url)" > short.txt
curl -s -o /dev/null -w "right away:       HTTP %{http_code}\n" "$(cat short.txt)"
sleep 75
curl -s -w "\nafter 75 seconds: HTTP %{http_code}\n" "$(cat short.txt)" | sed "s/<Details>.*//"
right away:       HTTP 200
<?xml version='1.0' encoding='UTF-8'?><Error><Code>ExpiredToken</Code><Message>Invalid argument.</Message>
after 75 seconds: HTTP 400

The same link gave HTTP 200 and then HTTP 400 with ExpiredToken. Fix: you cannot extend a signed URL; you sign a new one.

export BUCKET=cbc-gcs-lab-$PROJECT_ID
gcloud storage sign-url gs://$BUCKET/invoice-1002.txt --duration=10m --region=us-east1 \
  --impersonate-service-account=url-signer@$PROJECT_ID.iam.gserviceaccount.com \
  --format="value(signed_url)" > url.txt
curl -s -w "HTTP %{http_code}\n" "$(cat url.txt)"
Invoice 1002: 1 backpack
HTTP 200
Break it and fix it
A 1-minute link works, then fails with ExpiredToken; a new link works again

Clean up (delete everything)

Why: with versioning on, a normal delete keeps noncurrent versions (and you keep paying for them). --all-versions removes every generation, then the bucket. We also delete the url-signer account.

export BUCKET=cbc-gcs-lab-$PROJECT_ID
gcloud storage rm -r --all-versions gs://$BUCKET 2>&1 | tail -2
gcloud iam service-accounts delete url-signer@$PROJECT_ID.iam.gserviceaccount.com --quiet
echo "buckets left: $(gcloud storage buckets list --format="value(name)" | wc -l)"
echo "url-signer accounts left: $(gcloud iam service-accounts list --format="value(email)" | grep -c url-signer)"
Removing gs://cbc-gcs-lab-your-project-id/...
..........
deleted service account [url-signer@your-project-id.iam.gserviceaccount.com]
buckets left: 0
url-signer accounts left: 0
Clean up
0 buckets and 0 signer accounts left

buckets left: 0 and url-signer accounts left: 0 are your proof.

FAQ

Which storage class should I pick? Use Standard for files you read often or keep less than a month, and let a lifecycle policy move them colder as they age. Moving a file out of Nearline, Coldline or Archive early costs a fee for the rest of the minimum stay (30, 90 or 365 days).

What is Autoclass? A bucket setting that moves each object between classes by itself, based on how often it is read. It is simpler than writing rules when you cannot predict use.

How long can a signed URL last? Up to 7 days with the V4 signing that gcloud uses. Keep it as short as the job needs: minutes for a download button, hours for a shared report.

Is a signed URL safe to send in chat? Anyone who has it can download the file until it expires, so treat it like a temporary password. For uploads you can sign a URL for the PUT method instead.

Do I need a service account key file to sign? No. With impersonation, as in this lab, Google signs for you and no key file exists. Key files are easy to leak, so avoid them.

Does versioning cost more? Yes: each noncurrent version is stored and billed like a normal object. That is why rule 4 of our lifecycle policy deletes old versions.

Why does a lifecycle change not happen at once? Google evaluates the rules about once a day, and a change can take up to 24 hours to apply.

Troubleshooting

You seeWhyFix
HTTP 409 ... bucket name is not availableBucket names are global and someone has itAdd your project id or a random word to the name
Signed link gives AccessDenied ... does not have storage.objects.getThe bucket grant failed because the new account was not visible yetRun the grant again (the first until loop in Step 8 does this)
Failed to impersonate ... iam.serviceAccounts.getAccessToken deniedThe Token Creator role has not spread yetWait; use the until loop from Step 8
Failed to auto-detect the region for ...The signer can read objects but not the bucket settingsAdd --region=us-east1 (your bucket's region)
ExpiredToken with HTTP 400The signed URL has passed its timeSign a new link
SignatureDoesNotMatchThe link was changed or cut when copiedCopy the whole link again, in quotes
A deleted file still costsVersioning kept a noncurrent copygcloud storage ls --all-versions, then delete with --all-versions

What next

Prepared by the CareerByteCode Team from our tested lab, with AI help for the write-up.

Read next →Firestore Hands-On: A NoSQL Database for Apps⏱ 9 min

⭐ Keep learning with CareerByteCode

Hand-picked to go deeper on this topic
News, guides and updates from the CareerByteCode team
🏅 Contributor writer

The CareerByteCode team writes about what is new on careerbytecode.in: courses, labs, exams, events and the community, which is free to join for influencers and content creators in tech.

Get CareerByteCode Team's next blog by email

One email a week when they publish. Add the weekly Best of ByteBlog too. No spam, unsubscribe with one click.

We email you once to confirm. By subscribing you agree to the privacy policy.

💬 Comments (0)

Top · Newest

Sign in to comment

More from CareerByteCode Team

⭐ Recommended by CareerByteCode · BytePlay courseNVIDIA GPU AI Factory: Architecture, Engineering & Operationsโ‚น40,000Start learning →