Google Cloud
02 / 04

Storage: GCS, Cloud SQL & Firestore

Google Cloud: Storage Services

Cloud Storage (GCS)

GCS is Google's object storage — equivalent to AWS S3. Stores blobs, backups, static assets, ML datasets.

# Create bucket
gcloud storage buckets create gs://my-bucket \
  --location=europe-west1 \
  --default-storage-class=STANDARD \
  --uniform-bucket-level-access

# Upload / download
gcloud storage cp local-file.txt gs://my-bucket/
gcloud storage cp -r ./dist gs://my-bucket/website/
gcloud storage cp gs://my-bucket/file.txt ./

# Sync (like rsync)
gcloud storage rsync -r ./dist gs://my-bucket/website

# Set CORS for web access
gcloud storage buckets update gs://my-bucket --cors-file=cors.json

# Static website hosting
gcloud storage buckets update gs://my-bucket --web-main-page-suffix=index.html
// Node.js SDK
import { Storage } from '@google-cloud/storage'
const storage = new Storage()
const bucket = storage.bucket('my-bucket')

// Upload
await bucket.upload('./report.pdf', { destination: 'reports/2026/report.pdf' })

// Signed URL (time-limited access)
const [url] = await bucket.file('private/doc.pdf').getSignedUrl({
  action: 'read',
  expires: Date.now() + 15 * 60 * 1000,  // 15 minutes
})

// Stream upload
const file = bucket.file('uploads/image.jpg')
const writeStream = file.createWriteStream({ contentType: 'image/jpeg' })
readStream.pipe(writeStream)

Cloud SQL

  • Fully managed PostgreSQL, MySQL, or SQL Server — automatic backups, HA, read replicas

  • Create: gcloud sql instances create my-db --database-version=POSTGRES_15 --tier=db-f1-micro --region=europe-west1

  • Connect via: Cloud SQL Auth Proxy (recommended for apps), Private IP (VPC), public IP + SSL

  • Connection string via Secret Manager → mount as env var in Cloud Run

  • Read replicas: gcloud sql instances create my-db-replica --master-instance-name=my-db

  • Costs: per-instance-hour + storage + I/O operations. Use db-f1-micro for dev (free tier eligible)

Firestore

Firestore is a serverless NoSQL document database — native Firebase integration, real-time listeners, offline support.

import { Firestore } from '@google-cloud/firestore'
const db = new Firestore()

// Write
await db.collection('users').doc('alice').set({
  name: 'Alice',
  email: 'alice@example.com',
  createdAt: Firestore.Timestamp.now(),
})

// Read
const doc = await db.collection('users').doc('alice').get()
console.log(doc.data())

// Query
const query = await db.collection('users')
  .where('role', '==', 'admin')
  .orderBy('createdAt', 'desc')
  .limit(10)
  .get()

// Real-time listener (client-side)
db.collection('messages').onSnapshot(snapshot => {
  snapshot.docChanges().forEach(change => {
    if (change.type === 'added') console.log('New message:', change.doc.data())
  })
})

Keep your own version of these notes — editable, searchable, and organised by your stack.

Start free