r2 is a library and command line interface for working with Cloudflare's R2 Storage.
r2 [command] [flags]configure— Configure R2 accesscp— Copy an object from one R2 path to anotherhelp— Help about any commandls— List either all buckets or all objects in a bucketmb— Create an R2 bucketmv— Moves a local file or R2 object to another location locally or in R2.pipe— Stream data from stdin to an R2 objectpresign— Generate a pre-signed URL for a Cloudflare R2 objectrb— Remove an R2 bucketrm— Remove an object from an R2 bucketsync— Syncs directories and R2 prefixes.
-p, --profile— R2 profile to use (default "default")--config— Path to R2 config file (default "~/.r2")-h, --help— Help for any command
Help for any command can be obtained by running r2 help [command]. For example:
# Help for the configure command
r2 help configureThe sync command copies changed files between a local directory and an R2 prefix.
--include-from— Read include patterns from a file (patterns are relative to the sync source root)
Lines are glob patterns. # begins a comment. ** matches across directories. A trailing /
means "include everything under this directory".
Patterns are relative to the sync source root. For example, use db-dump/ not /d/db-dump/.
Example patterns.txt:
# Only include db dumps and SQL files
db-dump/
**/*.sql
Example usage:
r2 sync --include-from patterns.txt /d/ r2://backup/2026-02-02/The pipe command allows you to stream data from stdin directly to R2 without creating temporary files. This is useful for backup scripts, data pipelines, and real-time data processing. Note: Data is buffered in memory during upload.
<command> | r2 pipe r2://bucket/path# Stream text to R2
echo "Hello World" | r2 pipe r2://bucket/hello.txt
# Backup a database directly to R2
mysqldump mydb | r2 pipe r2://backups/db-backup.sql
# Compress and upload a directory
tar czf - /path/to/dir | r2 pipe r2://bucket/archive.tar.gz
# Stream from a file
cat large-file.bin | r2 pipe r2://bucket/large-file.bin
# Use with quiet mode
echo "data" | r2 pipe r2://bucket/file.txt --quiet--part-size— Part size for multipart upload in bytes (minimum 5MB, default 5MB)--concurrency— Number of concurrent upload threads (default 5)-q, --quiet— Suppress progress output
The r2 library can be used to interact with R2 from within your Go application. All library code
exists in the pkg directory and is well documented.
Documentation may be found here.
Uploading a file to a bucket:
package main
import (
r2 "github.com/erdos-one/r2/pkg"
)
func main() {
// Create client
config := r2.Config{
Profile: "default",
AccountID: "<ACCOUNT ID>",
AccessKeyID: "<ACCESS KEY ID>",
SecretAccessKey: "<SECRET ACCESS KEY>"
}
client := r2.Client(config)
// Connect to bucket
bucket := client.Bucket("my-bucket")
// Upload a file to the bucket
bucket.Upload("my-local-file.txt", "my-remote-file.txt")
}