Skip to content

Cloud Storage

Cloud Storage is GCP’s object storage service — store files, images, backups, static assets, and data lakes. It’s equivalent to AWS S3 or Azure Blob Storage.

ClassUse CaseMin DurationCost
StandardFrequently accessed dataNoneHighest
NearlineOnce a month or less30 daysLower
ColdlineOnce a quarter or less90 daysLower
ArchiveLong-term backups365 daysLowest
Terminal window
# Create a bucket (globally unique name required)
gcloud storage buckets create gs://my-bucket-name \
--location=europe-west2 \
--default-storage-class=standard

Bucket names are globally unique across all GCP customers.

Terminal window
# Upload a file
gcloud storage cp ./local-file.txt gs://my-bucket-name/
# Upload a directory recursively
gcloud storage cp -r ./dist/ gs://my-bucket-name/
# Download a file
gcloud storage cp gs://my-bucket-name/remote-file.txt ./
# List objects
gcloud storage ls gs://my-bucket-name/
gcloud storage ls -l gs://my-bucket-name/ # with sizes
# Delete an object
gcloud storage rm gs://my-bucket-name/old-file.txt

Uniform bucket-level access (recommended):

Terminal window
# Make a bucket publicly readable
gcloud storage buckets add-iam-policy-binding gs://my-bucket-name \
--member=allUsers \
--role=roles/storage.objectViewer
# Grant a user write access
gcloud storage buckets add-iam-policy-binding gs://my-bucket-name \
--member=user:user@example.com \
--role=roles/storage.objectAdmin

Object-level ACL (legacy):

Terminal window
gcloud storage objects update gs://my-bucket-name/file.txt \
--predefined-acl=publicRead
Terminal window
# Set the main page and 404 page
gcloud storage buckets update gs://my-bucket-name \
--web-main-page-suffix=index.html \
--web-error-page=404.html
# Make bucket public
gcloud storage buckets add-iam-policy-binding gs://my-bucket-name \
--member=allUsers \
--role=roles/storage.objectViewer

The bucket’s public URL is:

https://storage.googleapis.com/my-bucket-name/index.html

Use Cloud CDN or a load balancer in front for custom domains + HTTPS.

Automatically transition or delete objects:

{
"rule": [
{
"action": { "type": "SetStorageClass", "storageClass": "NEARLINE" },
"condition": { "age": 30 }
},
{
"action": { "type": "Delete" },
"condition": { "age": 365 }
}
]
}
Terminal window
gcloud storage buckets update gs://my-bucket-name \
--lifecycle-file=lifecycle.json

Generate time-limited URLs to grant temporary access without authentication:

Terminal window
gcloud storage sign-url gs://my-bucket-name/private-file.pdf \
--duration=1h \
--private-key-file=service-account-key.json

gcloud storage is the newer, recommended CLI. gsutil (older) is still widely used:

Terminal window
# gsutil equivalents
gsutil cp ./file.txt gs://my-bucket/
gsutil ls gs://my-bucket/
gsutil rm gs://my-bucket/file.txt

Backup to Cloud Storage:

Terminal window
# Database backup
pg_dump mydb | gzip | gcloud storage cp - gs://my-backups/db-$(date +%Y%m%d).sql.gz

Sync a local directory:

Terminal window
gcloud storage rsync -r ./build/ gs://my-bucket/