Persisting Data in Google Colab: Local Disk vs Drive vs GCS
Decide how to store data in Google Colab: local SSD, Google Drive, or Google Cloud Storage. A quick table, trade‑offs, and code examples show when to use each option and how to validate your setup.
13 Jul 2025, 12:35 UTC

The Decision
When you run a notebook in Google Colab you immediately face a storage dilemma. The runtime’s local disk at /content is fast but disappears on a reset or idle timeout. If you need data to survive beyond the current session you must hand it off to an external store. Google offers two primary persistent options: a free Google Drive mount and a paid Google Cloud Storage (GCS) bucket. Choosing the right one depends on three core constraints:
- Dataset size – How many gigabytes or terabytes do you need to keep?
- Budget & billing – Are you willing to enable a GCP project and pay for storage and egress?
- Collaboration & access – Do you need to share data with teammates or other services?
Below is a concise decision guide that maps these constraints to the appropriate storage backend.
Comparison Table
| Option | Speed | Persistence | Setup | Cost | Sharing |
|---|---|---|---|---|---|
Local Disk (/content) | Fastest – native SSD | None – lost on reset | None – built‑in | Zero | Not applicable |
| Google Drive (mount) | Medium – 1–3 GB/s depending on network | Persistent to your Google account | Mount via drive.mount(), 2‑step OAuth | Free up to 15 GB (shared with Gmail/Photos); paid plans available | Share folder or file links; easy for small teams |
| Google Cloud Storage | Medium‑slow – depends on bucket location and network | Persistent, highly durable (99.999999999%) | Requires GCP project, gcloud or client library | Storage and egress charges apply; free tier 5 GB | Fine‑grained IAM; can be shared with other GCP services |
Trade‑Offs
- Speed vs Persistence –
/contentis the fastest place to write intermediate checkpoints, but you must copy to Drive or GCS for long‑term storage. - Setup friction – Drive requires a user OAuth flow each session; GCS needs a GCP project and either a service account key or user auth.
- Cost & quota – Drive’s 15 GB free quota is shared with Gmail/Photos; exceeding it silently fails writes. GCS charges for storage, operations, and egress; large training datasets can quickly accrue costs.
- Collaboration – Drive’s folder sharing is intuitive for non‑technical teammates; GCS shares are IAM‑based and better for programmatic access.
- Security – Never store secrets in notebook cells. Use Colab Secrets or environment variables. For GCS, keep service‑account keys in a protected vault or use
google.colab.auth.authenticate_user()to avoid static keys.
Implementation Guide
Mounting Google Drive
# Run in a notebook cell
from google.colab import drive
drive.mount('/content/drive')
# Follow the OAuth link and paste the code
After mounting, your personal Drive appears under /content/drive/MyDrive. Use standard os or shutil operations to read/write.
Using Google Cloud Storage
First authenticate. The quickest path is user OAuth, which works for most notebooks.
# Authenticate the current notebook
from google.colab import auth
auth.authenticate_user()
Next, install the client library if it’s not already present (Colab images usually ship it, but double‑check).
!pip install google-cloud-storage --quiet
Now you can upload a file to a bucket named {BUCKET_NAME}:
from google.cloud import storage
client = storage.Client()
bucket = client.bucket('{BUCKET_NAME}')
blob = bucket.blob('data/example.csv')
blob.upload_from_filename('/content/example.csv')
To download:
blob.download_to_filename('/content/downloaded.csv')
Alternatively, use gsutil, which is preinstalled:
# List buckets
!gsutil ls
# Copy a file to GCS
!gsutil cp /content/example.csv gs://{BUCKET_NAME}/data/
Local Disk Use‑Case
For temporary caches, model checkpoints, or data that you’ll regenerate, keep everything under /content. Example: copying a large dataset to a local cache for faster subsequent reads.
import shutil, os
src = '/content/drive/MyDrive/large_dataset'
cache_dir = '/content/cache'
shutil.copytree(src, cache_dir)
Validation Checklist
- Verify Mounts – Run
!df -h /content /content/driveand!gsutil du -s gs://{BUCKET_NAME}to confirm space and connectivity. - Existence Check –
os.path.exists('/content/drive/MyDrive/expected_file.txt')should returnTrueafter a write. - Speed Benchmark – Use the
%%timemagic to copy a representative file and compare throughputs:
%%time
shutil.copy('/content/drive/MyDrive/large_file.bin', '/content/')
%%time
!gsutil cp gs://{BUCKET_NAME}/large_file.bin /content/
!df -h /content/drive shows used vs available. For GCS, !gsutil du -s gs://{BUCKET_NAME} shows total bytes stored.os.getenv('GOOGLE_APPLICATION_CREDENTIALS') if you’re loading a service‑account JSON.Practical Tips & Caveats
- Always re‑run mount commands after a runtime restart; the session state is lost.
- Drive writes can silently fail if you exceed the quota; check
!df -hbefore large jobs. - GCS egress to the internet incurs costs; if your notebook is in the same region as the bucket, egress is free.
- Use
google.colab.files.upload()only for small, one‑off files; for large datasets prefer Drive or GCS. - For multi‑user pipelines, consider a shared GCS bucket with IAM policies instead of a personal Drive folder.
- Keep
/contentas a cache: write intermediate checkpoints there, then copy to Drive or GCS when you’re ready to persist.
Conclusion
In short: use /content for speed and temporary work; mount Drive for free, easy persistence when data fits the 15 GB quota; and switch to GCS when you need large, durable datasets, programmatic access, or team‑wide sharing with fine‑grained IAM. Validate your choice with a quick benchmark and quota check, and always keep secrets out of notebook cells.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.