Serving Private Files from Backblaze B2 in FastAPI
I recently needed to serve private files (like PDFs and images) from my FastAPI backend, but I didn’t want to use AWS S3. Instead, I went with Backblaze B2—a cost-effective and S3-compatible object storage service.
In this post, I’ll walk you through how I used B2's private buckets and generated secure download links that FastAPI could serve dynamically.
🎯 The Goal
- Store files in a private B2 bucket
- Use FastAPI to securely serve/download those files
- Prevent direct access to the bucket (no public URLs)
- Avoid caching or temporary local storage
Setting Up B2
- Create a B2 bucket.
- Set the bucket to private.
- Create an Application Key with appropriate permissions.
Connecting to B2 from Python
I used the official b2sdk
to authenticate and fetch files:
Top comments (0)