A robust, reusable TypeScript uploader for Cloudflare R2 that works with both Bun.js and Node.js.
- ✅ Upload files and folders to Cloudflare R2
- ✅ Compatible with both Bun.js and Node.js
- ✅ Automatic content-type detection
- ✅ Directory prefix support for organization
- ✅ Public URL generation
- ✅ CSV result exports
- ✅ Comprehensive error handling
- ✅ Reusable as a module
- Bun.js (v1.0+) or Node.js (v18+)
- A Cloudflare account with R2 enabled
- Log in to your Cloudflare Dashboard
- Navigate to R2 Object Storage
- Click Create bucket
- Enter a unique bucket name (e.g.,
my-uploads) - Select your preferred location (or use Automatic)
- Click Create bucket
To make uploaded files publicly accessible:
- Go to your bucket settings
- Under Public Development URL, click Enable
- Type
allowto confirm - Your public URL will be:
https://{bucket-name}.{account-id}.r2.dev
- In bucket settings, go to Custom Domains
- Click Add and enter your domain
- Cloudflare will create the necessary DNS records
- Use your custom domain as
R2_PUBLIC_URLin.env
- In the R2 dashboard, click Manage R2 API Tokens
- Click Create API Token
- Configure the token:
- Token Name:
r2-uploader - Permissions: Select Object Read and Write
- Bucket: Choose your bucket or All buckets
- Token Name:
- Click Create API Token
- IMPORTANT: Copy the Access Key ID and Secret Access Key immediately (they won't be shown again)
- Note your Account ID (visible in the dashboard URL or R2 overview)