Store files in the cloud with Cloud Storage: create buckets, upload and download objects, pick storage classes to cut costs, automate cleanup with lifecycle rules, turn on versioning, and host a static website.
Cloud Storage keeps files ("objects") in "buckets" (top-level containers). Why: it is the default place for anything — images, backups, logs, static sites — with effectively unlimited capacity and high durability. Note: bucket names are globally unique across all of Google Cloud, so add something distinctive.
Create a bucket in a region (the name must be globally unique)
gcloud storage buckets create gs://learn-uploads-7f3k \
--location us-central1Upload a file (it becomes an "object")
gcloud storage cp ./photo.jpg gs://learn-uploads-7f3k/List the bucket, then download the file back
gcloud storage ls gs://learn-uploads-7f3k/gcloud storage cp gs://learn-uploads-7f3k/photo.jpg ./downloaded.jpggcloud storage rsync copies only what changed between a local folder and a bucket (or vice-versa). Why: it is the fast, repeatable way to push a whole directory — a built website, a backup set — without re-uploading unchanged files.
Mirror a local folder into the bucket
gcloud storage rsync ./public gs://learn-uploads-7f3k/site --recursiveAdd --delete-unmatched-destination-objects to remove bucket files that no longer exist locally
gcloud storage rsync ./public gs://learn-uploads-7f3k/site \
--recursive --delete-unmatched-destination-objectsStorage classes trade retrieval cost for storage cost: Standard (hot, default), Nearline (accessed ~monthly), Coldline (~quarterly), Archive (rarely, cheapest to store). Why: matching the class to how often data is read can cut the bill dramatically. You set it per object or as a bucket default.
Upload an object directly into the Nearline class
gcloud storage cp ./old-report.pdf gs://learn-uploads-7f3k/ \
--storage-class NEARLINEChange an existing object's class to Archive
gcloud storage objects update gs://learn-uploads-7f3k/old-report.pdf \
--storage-class ARCHIVEA lifecycle rule transitions or deletes objects automatically as they age. Why: instead of cleaning up by hand, you say "Coldline after 90 days, delete after a year" once, and Cloud Storage enforces it forever.
Save this as lifecycle.json
{
"rule": [
{
"action": { "type": "SetStorageClass", "storageClass": "COLDLINE" },
"condition": { "age": 90 }
},
{
"action": { "type": "Delete" },
"condition": { "age": 365 }
}
]
}gcloud storage buckets update gs://learn-uploads-7f3k \
--lifecycle-file lifecycle.jsonWith versioning on, overwriting or deleting an object keeps the old copy as a noncurrent version. Why: it protects against accidental deletes and bad overwrites — you can roll any object back. Note: old versions keep costing storage, so pair this with a lifecycle rule to expire them.
Turn versioning on
gcloud storage buckets update gs://learn-uploads-7f3k --versioningList all versions (including noncurrent ones)
gcloud storage ls --all-versions gs://learn-uploads-7f3k/Buckets are private by default. You grant access with IAM bindings on the bucket. Why: to serve a public website you allow allUsers to read objects; for private data you grant only specific identities. Uniform bucket-level access keeps permissions simple and consistent.
Make objects publicly readable (only for a public website/assets)
gcloud storage buckets add-iam-policy-binding gs://learn-uploads-7f3k \
--member=allUsers --role=roles/storage.objectViewerGrant a service account read access instead (for private data)
gcloud storage buckets add-iam-policy-binding gs://learn-uploads-7f3k \
--member="serviceAccount:app-runner@learn-gcp-7f3k.iam.gserviceaccount.com" \
--role=roles/storage.objectViewerCloud Storage can serve a folder of HTML/CSS/JS straight to browsers. Why: it is a cheap way to host a static site. For HTTPS on a custom domain you put the HTTP(S) load balancer or Cloud CDN (next lessons) in front, with the bucket as the backend.
Set the index and 404 pages for website serving
gcloud storage buckets update gs://learn-uploads-7f3k \
--web-main-page-suffix index.html --web-error-page error.htmlUpload the site (bucket must allow public read, as above)
gcloud storage rsync ./dist gs://learn-uploads-7f3k --recursive