R2 Uploader
A CLI tool for uploading compiled binaries to Cloudflare R2 storage and managing Cloudflare cache.
Overview
This tool provides two main functionalities:
- Upload: Upload compiled Rust binaries to Cloudflare R2 storage
- Purge: Purge Cloudflare cache for specified URLs
The upload feature stores binaries in both versioned and latest paths for easy access.
Installation
Install the tool using Cargo:
This will install the r2 command globally on your system.
Usage
Required Environment Variables
For Binary Upload
# Cloudflare R2 bucket name (required)
# Cloudflare account ID (required)
# Authentication method: API token or API key + email
# Method 1: API token (recommended)
# Method 2: API key + email
For Cache Purging
# Cloudflare zone ID (required for cache purging)
# Cache purge authentication
Commands
Upload Binary
Upload compiled binaries to Cloudflare R2 storage.
Basic Usage:
# Upload binary with auto-detected version from Cargo.toml
# Upload binary with specific version
Advanced Options:
# Upload binary from a specific file path
# Upload binary from a different target directory
Upload Options:
| Option | Description | Required | Default Value |
|---|---|---|---|
--name |
Name of the binary to upload | Yes | - |
--binary-version |
Version of the binary | No | Auto-detected |
--file-path |
Custom file path to upload | No | - |
--target-dir |
Target directory to search for the binary | No | ./target/release |
Purge Cache
Purge Cloudflare cache for specified URLs.
# Purge cache for multiple URLs
# Purge cache for a single URL
Purge Options:
| Option | Description | Required |
|---|---|---|
--files |
Comma-separated list of URLs | Yes |
-f |
Short form of --files |
Yes |
Help
Get help for any command:
# General help
# Upload command help
# Purge command help
How It Works
Binary Upload
- Reads the binary file from the specified path or target directory
- Automatically detects version from Cargo.toml if not specified
- Uses the Cloudflare R2 API to upload the file to two locations:
- Versioned path:
bin/{name}/{version}/{name} - Latest path:
bin/latest/{name}
- Versioned path:
Cache Purging
- Takes a list of URLs to purge from Cloudflare cache
- Uses the Cloudflare cache purge API to invalidate the specified URLs
- Provides detailed feedback on the purge operation
R2 Bucket Structure
Uploaded binaries are stored with the following structure:
{bucket_name}/
└── bin/
├── {binary_name}/
│ └── {version}/
│ └── {binary_name}
└── latest/
└── {binary_name}
Example:
- Versioned:
{bucket_name}/bin/my-app/0.3.0/my-app - Latest:
{bucket_name}/bin/latest/my-app
Examples
Complete Upload Workflow
# Set environment variables
# Build your binary
# Upload to R2 (version auto-detected from Cargo.toml)
# Upload with specific version
Cache Management
# Set cache purge environment variables
# Purge cache for updated binaries