File Storage and Uploads with Object Storage
Accept, store and serve user files safely using object storage like S3 instead of your server's disk.
Intermediate · about 5 hours · difficulty 2/5
Learn first: Resource-Oriented REST APIs, Authorization: Roles and Permissions
Sooner or later users upload avatars, PDFs or videos. Storing them on the app server's local disk breaks as soon as you run two instances or redeploy a container. The standard answer is object storage: Amazon S3 and its compatible cousins (Cloudflare R2, Google Cloud Storage, Azure Blob, self-hosted MinIO).
The mental model
Object storage is a flat key-value store for blobs. A bucket holds objects, each addressed by a key such as users/42/avatar.png, with metadata like content type. There are no real folders, just key prefixes. It is cheap, durable and scales without you thinking about disks.
Upload patterns
- Proxy upload: the client sends the file to your API, which streams it to storage. Simple, but your server carries all the bytes.
- Presigned URL upload (recommended): your API checks permissions, then returns a short-lived signed URL; the client uploads directly to the bucket. Your server only stores the key in the database.
- For large files use multipart uploads so a failed chunk can be retried without restarting.
Serving files
Keep buckets private by default. Serve public assets through a CDN, and serve private files with short-lived presigned download URLs after an authorization check.
Security pitfalls
- Trusting the client's filename or
Content-Type. Generate your own keys and validate type and size on the server. - Serving user uploads from your main domain, which can enable stored XSS. Use a separate domain or force
Content-Disposition: attachment. - Public buckets created "just for testing" that leak customer data.
You are ready to move on when you can implement presigned uploads with size limits, record the object key in your database and serve the file back only to its owner.
Key concepts
- Buckets, objects and keys
- Presigned upload and download URLs
- Multipart and resumable uploads
- Validating type and size
- Serving files through a CDN
- Image resizing pipelines
- Lifecycle rules and storage classes
- S3-compatible stores (R2, MinIO)
Mini project
Add profile pictures to an API: issue a presigned upload URL limited to 2 MB images, store the key, generate a thumbnail in a background step and serve it via a presigned download URL.
Check yourself
Why is uploading directly from the browser to S3 with a presigned URL usually better than streaming the file through your API?
Free resources
- Amazon S3 User Guide (AWS)
- Working with presigned URLs (AWS)
- OWASP File Upload Cheat Sheet (OWASP)
- Cloudflare R2 documentation (Cloudflare)