Cloudflare R2 & S3

With VELA_STORAGE=r2, file contents live in a private Cloudflare R2 bucket — or any S3-compatible service — while SQLite metadata stays on the server. The API, the CLI, URLs and access rules do not change; only where the bytes rest and how they travel.

When to use it

  • Your server's disk is small, but you need to store a lot.
  • You want contents in object storage with its own durability and replication.
  • You want downloads to bypass your server's bandwidth (redirect mode).

The trade-offs: uploads and (in proxy mode) downloads pass through velad, multipart uploads need memory, and backups become a two-part job. Both backends behave identically through the API, and you can migrate in either direction.

What goes where

DataLocation
Paths, metadata, keys, links, trash, policies/data/vela.db on the server
File contents<bucket>/<VELA_R2_PREFIX><shard>/<blob-id>, e.g. blobs/r7/c4/01m4tscd50ry5wfktp4mx4r7c4
Temporary upload filesnone — uploads stream directly as multipart

Object keys contain only random blob IDs, never file names or paths, and renames never touch the bucket. SQLite is always the source of truth.

Set up the bucket

  1. Create a private bucket

    In the Cloudflare dashboard create a bucket, for example vela-store, with a location hint near your server. Keep public access off, leave the r2.dev subdomain disabled and do not attach a custom domain. Everything must go through Vela.

  2. Add a lifecycle rule

    Add Abort incomplete multipart uploads after 1 day. The janitor already aborts Vela's own stale uploads; the rule covers crashes in between.

  3. Create a bucket-scoped token

    Under R2 → Manage API tokens → Create, grant Object Read & Write restricted to this bucket only, with no account permissions. Note the access key ID, secret access key and your account ID.

  4. Configure Vela

    Start from examples/env/r2.env.example:

    VELA_PUBLIC_URL=https://store.example.com
    VELA_STORAGE=r2
    VELA_ACCEL_PREFIX=
    
    VELA_R2_ACCOUNT_ID=0123456789abcdef0123456789abcdef
    VELA_R2_BUCKET=vela-store
    VELA_R2_ACCESS_KEY_ID=CHANGE-ME
    VELA_R2_SECRET_ACCESS_KEY=CHANGE-ME
    VELA_R2_PREFIX=blobs/
    VELA_R2_DELIVERY=proxy
  5. Start and check

    VELA_IMAGE=xchunk/vela:0.9.0 ./examples/docker/run-r2.sh
    docker logs vela | grep 'velad started'     # delivery=r2 proxy
    vela put test.bin check/test.bin --private  # proves write access
    vela get check/test.bin - | sha256sum

    Startup runs a HeadBucket check, but that cannot prove write permission — only a real upload does.

Credentials are explicitThe backend reads only the VELA_R2_* variables. It ignores AWS_* environment variables, ~/.aws files and instance metadata, and credentials are never accepted as command-line flags. Keep the env file mode 0600 and out of version control.

Configuration

VariableDefaultMeaning
VELA_STORAGEfsr2 selects this backend (also for generic S3)
VELA_R2_ACCOUNT_IDemptyCloudflare account; required unless VELA_R2_ENDPOINT is set
VELA_R2_ENDPOINThttps://<account>.r2.cloudflarestorage.comBare http(s) origin of the S3 API
VELA_R2_REGIONautoSigning region; set your provider's region for other services
VELA_R2_BUCKETrequiredExisting private bucket
VELA_R2_PREFIXblobs/Relative managed prefix, must end in /. Dedicate it to one Vela store.
VELA_R2_ACCESS_KEY_IDrequiredBucket-scoped access key
VELA_R2_SECRET_ACCESS_KEYrequiredBucket-scoped secret
VELA_R2_DELIVERYproxyproxy or redirect
VELA_R2_PRESIGN_TTL5mRedirect grant lifetime, 1 s – 1 h (Go duration syntax, e.g. 90s, 10m)
VELA_R2_PART_SIZE8MiBMultipart part size, 5–64 MiB
VELA_R2_UPLOAD_CONCURRENCY2Parts uploaded in parallel per upload, 1–16
VELA_R2_PATH_STYLEtruePath-style addressing; false for virtual-hosted buckets
VELA_MAX_CONCURRENT_UPLOADS4Active uploads (1–1024); extra uploads wait, cancellable by the client

R2 mode requires an empty VELA_ACCEL_PREFIX; a non-empty one fails at startup.

Delivery: proxy or redirect

proxy (default)redirect
HowGo fetches from the bucket and streams to the clientGo answers 302 to a short-lived presigned GET
BandwidthThrough your serverDirectly from the bucket
Client seesOnly your hostnameThe S3 hostname after the redirect
RangesSingle ranges (206); multi-range falls back to a full 200Handled by the bucket
RevocationImmediateAn issued grant stays valid until its short TTL ends
RequirementProxy buffering/temp files disabledClients can reach the S3 origin

Proxy mode details

HEAD requests and matching conditional requests are answered from SQLite without touching the bucket. Size and content type come from the database, the ETag is "sha256:<hex>", and all policy headers are set by Go. If the client disconnects, the bucket read is cancelled. Keep proxy_buffering off and proxy_max_temp_file_size 0 in nginx so downloads never spool to the server disk.

Redirect mode details

  • Go checks access exactly as usual, then redirects. The presigned URL carries the response content type, disposition (with filename*) and cache policy, so browsers receive the right headers from the bucket.
  • The 302 itself is Cache-Control: private, no-store and Referrer-Policy: no-referrer; crawler hints (X-Robots-Tag) are emitted on the redirect.
  • Grants are capped at the object's or link's own expiry, so a redirect never extends access.
  • Near expiry, and for any request with conditional headers (If-Match, If-None-Match, If-Range, timestamp conditions), Vela falls back to proxy delivery so its SHA-256 validators apply.
  • The Go client and the CLI strip Authorization and Referer when following redirects, so keys never reach the bucket.

An S3 grant that was already issued remains valid until its TTL — at most VELA_R2_PRESIGN_TTL — even if you revoke the link or make the file private in the meantime. Keep the TTL short.

Other S3-compatible services

The same backend works with other S3 APIs. Set an explicit endpoint and the provider's signing region; the account ID can stay empty:

VELA_STORAGE=r2
VELA_R2_ENDPOINT=https://s3.eu-central-1.example-provider.com
VELA_R2_REGION=eu-central-1
VELA_R2_BUCKET=vela-store
VELA_R2_PATH_STYLE=false          # if the provider requires virtual-hosted addressing
VELA_R2_ACCESS_KEY_ID=CHANGE-ME
VELA_R2_SECRET_ACCESS_KEY=CHANGE-ME

MinIO is exercised by the test suite. Other providers need their own verification: test small and multipart uploads, downloaded SHA-256, presigned URLs (for redirect mode), interrupted uploads and janitor cleanup on an isolated bucket or prefix before production.

Memory planning

Uploads stream as multipart without staging whole files, but the pinned AWS SDK buffers parts in memory. The payload buffers are approximately:

part size × (part concurrency + 2) × active uploads
= 8 MiB × (2 + 2) × 4 = 128 MiB at the defaults

Growth of the first part creates extra transient allocations, so reserve at least:

part size × (part concurrency + 4) × active uploads
= 8 MiB × (2 + 4) × 4 = 192 MiB at the defaults

plus headroom for garbage collection, TLS, SQLite and request buffers. This is a capacity-planning allowance, not a hard RSS bound. On a small VPS, lower VELA_MAX_CONCURRENT_UPLOADS first; extra uploads simply wait.

Upload size limits

S3 multipart uploads are capped at 10,000 parts and Vela uses a fixed part size, so the largest possible object is VELA_R2_PART_SIZE × 10000. Startup rejects a VELA_MAX_UPLOAD or VELA_MAX_CREATE above that ceiling.

Part sizeMaximum objectReserve at defaults (2 parts, 4 uploads)
5MiB48.8 GiB120 MiB
8MiB (default)78.125 GiB192 MiB
11MiB107.4 GiB264 MiB
16MiB156.25 GiB384 MiB
64MiB625 GiB1.5 GiB

A 100 GiB limit therefore needs at least 11 MiB parts and a larger memory allowance. Migration from the filesystem preflights every referenced size against the ceiling before copying anything.

Timeouts, retries and health

  • Requests have connection and header deadlines and at most three SDK attempts. There is no total deadline on streaming bodies, so large transfers are not cut off.
  • A failed multipart write is aborted; a completed object with a wrong checksum is deleted.
  • Backend errors are logged without raw SDK messages or presigned URLs.
  • /healthz includes a bucket HeadBucket check, cached for 45 seconds so health probes do not hammer the API.

Backups and migration

In R2 mode velad backup writes only the SQLite snapshot. A consistent disaster-recovery copy needs the database and a matching copy of the bucket prefix, taken while Vela is stopped. See R2 backups for a complete procedure, and Storage Migration to move between the filesystem and R2.