Build a Cheap Cold Storage Image Storage System with Backblaze B2 and Cloudflare Worker
A detailed guide to building a low-cost cold storage image storage system using Backblaze B2 and Cloudflare Worker, optimizing cost, security, and remote data access.
Build a Cheap Cold Storage Image Storage System with Backblaze B2 and Cloudflare Worker
In the digital age, storing images, videos, and multimedia data is increasingly important. However, traditional storage solutions like AWS S3, Google Cloud Storage, or Azure Blob can quickly become expensive as data volume grows. To address this, many businesses and developers have turned to low-cost cold storage solutions, where Backblaze B2 and Cloudflare Worker stand out as two powerful tools. This guide will walk you through building an effective, secure, and cost-efficient cold storage image system by combining these two services.
1. Introduction to Backblaze B2 and Cloudflare Worker
Backblaze B2 is a cloud storage service with extremely low costs, ideal for infrequently accessed data (cold storage). At just 0.005 USD/GB/month, B2 offers unlimited storage, high security, and access via API or web interface. It’s a perfect choice for image files, videos, backups, or historical data.
Cloudflare Worker is a serverless platform running on Cloudflare’s network, allowing you to deploy JavaScript/TypeScript code without managing servers. With fast processing, API integration, and CORS support, Cloudflare Worker is ideal for creating a secure and efficient interface to access data from Backblaze B2.
2. Prerequisites and Setup
Prerequisites
- Backblaze B2 account (free registration available)
- Cloudflare account (free registration available)
backblaze-b2library or compatible SDK- Basics of JavaScript/TypeScript and API knowledge
3. Deployment Guide
Step 1: Create a Bucket on Backblaze B2
- Log in to Backblaze B2.
- Create a new bucket, e.g.,
my-photos-cold-storage. - Choose the storage type as Standard or Cold Storage based on your needs.
Step 2: Create an API Key on Backblaze B2
- Go to Application Keys in the Backblaze dashboard.
- Create an API key with
readandwritepermissions. - Save your
accountIDandapplicationKey.
Step 3: Write a Cloudflare Worker to Access Backblaze B2
- Create a new Worker on Cloudflare Workers.
- Set up the code as follows:
// worker.js
const B2_ACCOUNT_ID = 'YOUR_B2_ACCOUNT_ID';
const B2_APPLICATION_KEY = 'YOUR_B2_APPLICATION_KEY';
const B2_BUCKET_NAME = 'my-photos-cold-storage';
async function getB2AuthToken() {
const response = await fetch(`https://api.backblaze.com/b2api/v2/b2_authorize_account`, {
method: 'POST',
headers: {
'Authorization': `Basic ${btoa(`${B2_ACCOUNT_ID}:${B2_APPLICATION_KEY}`)}`,
},
});
const data = await response.json();
return data.authorizationToken;
}
async function listB2Files(authToken) {
const response = await fetch(`https://api.backblaze.com/b2api/v2/b2_list_files/b2_list_files?bucketId=${B2_BUCKET_NAME}`, {
headers: {
'Authorization': authToken,
},
});
return await response.json();
}
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request));
});
async function handleRequest(request) {
const url = new URL(request.url);
const path = url.pathname.substring(1); // Remove leading slash
if (path === 'list') {
const authToken = await getB2AuthToken();
const files = await listB2Files(authToken);
return new Response(JSON.stringify(files), {
headers: { 'Content-Type': 'application/json' },
});
}
// Handle file upload or download logic here
return new Response('Not implemented', { status: 404 });
}
Step 4: Configure Cloudflare Worker
- Log in to the Cloudflare Dashboard.
- Select the created Worker.
- Replace environment variables
B2_ACCOUNT_ID,B2_APPLICATION_KEY, andB2_BUCKET_NAMEwith actual values. - Register the Worker with your domain (e.g.,
storage.yourdomain.com).
4. Testing and Verification
Verify the Setup
- Visit
https://your-worker-domain.com/listto check the file list. - Run the following curl command to test the API:
curl -X GET https://your-worker-domain.com/list
Ensure Security
- Never store API keys in source code.
- Use environment variables in Cloudflare Worker to protect sensitive information.
- Set up CORS and token authentication if needed.
5. Optimization and Scaling
Cost Optimization
- Use Backblaze B2 Cold Storage to reduce storage costs for infrequently accessed files.
- Apply lifecycle rules to automatically move files from Standard to Cold Storage.
Scaling the System
- Integrate with ntfy.sh to receive notifications when new files are uploaded.
- Deploy self-hosted AI tools on a VPS to automatically process images.
6. Conclusion
Combining Backblaze B2 and Cloudflare Worker creates a low-cost, secure, and scalable cold storage image storage system. With low costs, API access, and serverless flexibility, this solution is ideal for small businesses and individual developers.
To learn more about efficient storage solutions, check out: Building a Push Notification System from Storage with ntfy.sh.
Related infrastructure & engineering guides: High-RAM Cloud VPS for Self-Hosted LLMs & API Gateways
High-RAM Cloud VPS Plans Optimized for AI, Databases & Cache
Enterprise RAID 10 NVMe SSDs, CMC & FPT District 9 uplinks with Anycast Anti-DDoS and 99.99% SLA.
VPS-4E
10% VAT included
- CPU: 4 vCPU
- RAM: 8 GB
- SSD: 50 GB NVMe
