A Cloudflare Worker that serves JSON files from R2 storage, with admin endpoints for uploading and deleting files.
- Serve JSON files directly from R2 storage
- Admin endpoints for uploading and deleting files
- Support for presigned URLs for direct browser uploads
- CORS support for cross-origin requests
- API key authentication for admin operations
- Node.js (v16 or later)
- Wrangler CLI
- A Cloudflare account with Workers and R2 enabled
-
Install dependencies:
npm install
-
Create an R2 bucket:
wrangler r2 bucket create json-files
-
Set your admin API key:
wrangler secret put ADMIN_API_KEY
-
Deploy the worker:
npm run deploy
GET /{filename}- Serve a JSON fileGET /- API documentation
PUT /upload/{filename}- Upload a JSON fileDELETE /delete/{filename}- Delete a JSON fileGET /presigned-url/{filename}- Get a presigned URL for direct upload
curl https://your-worker.your-subdomain.workers.dev/data.jsoncurl -X PUT \
-H "x-api-key: your-api-key" \
-H "Content-Type: application/json" \
-d '{"key": "value"}' \
https://your-worker.your-subdomain.workers.dev/upload/data.jsoncurl -X DELETE \
-H "x-api-key: your-api-key" \
https://your-worker.your-subdomain.workers.dev/delete/data.jsoncurl -H "x-api-key: your-api-key" \
https://your-worker.your-subdomain.workers.dev/presigned-url/data.jsonRun the worker locally:
npm run dev- The admin API key should be kept secure and not committed to version control
- Consider implementing rate limiting for production use
- Review CORS settings for your specific use case
- Consider implementing additional authentication methods for production use