https://github.com/0xSalik/R2-Worker-API
Client-side app for Cloudflare R2 bucket using Cloudflare workers.
https://github.com/0xSalik/R2-Worker-API
bucket cdn cloudflare cloudflare-r2 cloudflare-r2-api cloudflare-workers kv-namespace r2 r2-api s3 workers workers-kv
Last synced: 11 months ago
JSON representation
Client-side app for Cloudflare R2 bucket using Cloudflare workers.
- Host: GitHub
- URL: https://github.com/0xSalik/R2-Worker-API
- Owner: 0xSalik
- License: mit
- Created: 2022-09-10T09:31:49.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2024-08-15T16:14:46.000Z (over 1 year ago)
- Last Synced: 2024-08-15T18:13:42.985Z (over 1 year ago)
- Topics: bucket, cdn, cloudflare, cloudflare-r2, cloudflare-r2-api, cloudflare-workers, kv-namespace, r2, r2-api, s3, workers, workers-kv
- Language: JavaScript
- Homepage:
- Size: 471 KB
- Stars: 15
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
## WORKER TO INTERACT WITH CLOUDFLARE R2 (S3) BUCKET AS AN API.
For a full tutorial on how to set this up you can check my [blog post](https://blog.salikkhan.com/how-to-set-up-a-cdn-using-cloudflare-r2-and-workers/).
This worker acts as an client-facing api for your R2 bucket. You can use it to upload or modify files on your bucket directly using an authentication key and serve files without having to authenticate, basically use it as a CDN.
## Setup
- Create a worker and an R2 bucket on the cloudflare dashboard.
- Create an authentication key and store it as a variable in your worker settings with the name `AUTH_KEY_SECRET`. You will use this key to authenticate with your api.
- Install wrangler and sign-in. ([Read official docs for instructions](https://developers.cloudflare.com/workers/wrangler/install-and-update/)).
- Edit the `wrangler.toml` file and add your worker name, account id and bucket name to it (you can get your account id with `$ wrangler whoami`).
- Deploy the worker with `$ wrangler publish`.
## Using the API
_Replace `` with your actual key (remove the angle brackets), and `example.workers.dev` with your own worker route._
```bash
# Uploading binary data using curl
$ curl https://example.workers.dev/ -X PUT --header 'X-Custom-Auth-Key: ' --data-binary 'test'
# Uploading files using curl
$ curl https://example.workers.dev/file.ext -X PUT --header 'X-Custom-Auth-Key: ' --upload-file ./file.ext
# Deleting a file using curl
$ curl https://example.workers.dev/file.ext -X DELETE --header 'X-Custom-Auth-Key: '
```
You can also use a custom domain by routing it to your worker from the cloudflare dashboard. (The custom domain needs to be in the same zone as the worker)
That's it. You can just use curl or write an app in a language of your choice to interact with the api.