Introduction
Stripes digital downloads product is simple to integrate but introduces three hidden costs: platform latency for non-US regions, payout delays, and geographic filtering that excludes wallets in sanctioned or high-fee jurisdictions. Self-hosted R2 buckets and Workers remove the platform, but only if caching, signing, and key management are handled correctly. This article focuses on the technical implementation rather than marketing claims, because latency and cost are measurable: a 128 KB PDF served from R2 with KV caching over Cloudflares edge network should reach <50 ms globally, and total cost per thousand requests should be <$0.002 for storage plus <$0.00003 for egress when cached.
Architecture Overview
The stack has four layers:
1. Object Storage: Single R2 bucket with versioning and lifecycle rules that move older products to Glacier-like storage after 30 days of no access. 2. Edge Delivery: Worker that publishes the product at /download/:productId using a deterministic URL format. The Worker front-runs the request with a KV cache (R2 to KV mirror) to avoid S3 GET calls on cache hits. 3. Access Control: Short-lived pre-signed URLs generated client-side via Ed25519 challenge-response. The signing key lives in a Worker secret, never leaves the edge, and rotates every 24 hours. 4. Commerce Rail: On-chain payment with Lightning invoices or onchain Bitcoin address. The payment processor is a minimal service: it listens to the Webhook for paid invoices, writes a JWT to a Durable Object namespace, and expires it after 24 hours. No custodial balance, no payout delay.
The four layers compose without platform-specific SDKs; Workers, R2, KV, and Durable Objects are all in the same Cloudflare account and billed under a single invoice.
Implementation Details
Bucket Setup
Create the bucket with CORS policy that allows only HEAD and GET on /download/:productId. Turn on versioning and add a lifecycle rule that transitions objects to R2 Glacier after 30 days of no GET requests. This reduces storage cost from $0.015/GB to $0.0036/GB without user-visible changes.
Worker Endpoint
The Worker exports a route /download/:productId that first checks KV for a cached {url,exp}. If present and not expired, it returns 302 to the pre-signed URL. If missing, it generates a new pre-signed URL using the Worker secret and writes it to KV with a 24-hour TTL. The pre-signed URL uses the sub-resource ?X-Amz-Signature= token which R2 validates.
Edge caching is critical: one R2 GET from the origin worker costs $0.0004 per request, while a cache hit at the edge costs $0.000002 (the KV read). With a 90 % hit rate, the egress bill drops by 90 % even though the client still sees millisecond latency.
Pre-Signing without Secrets Exposure
The Worker uses a deterministic signing routine derived from a secret stored in a Worker secret. The secret never leaves the edge location, so a rogue Lambda@Edge does not expose it. The routine signs a payload {productId, expiresAt} with Ed25519 and returns a token that the Worker inserts into the R2 pre-signed URL. The token is 64 bytes—small enough to fit in a query string without URL-encoding overhead.
Payment Finality
The commerce service listens to a Lightning invoice Webhook. When paid, it creates a JWT in a Durable Object namespace keyed by paymentHash. The Worker endpoint verifies the JWT signature and productId before returning the pre-signed URL. The JWT is single-use and expires after 24 hours, matching the pre-signed URL TTL.
The payment rail itself is outside the Workers runtime to avoid CPU billing during crypto operations. A serverless function (Fly.io or Railway) handles invoice creation and Webhook verification, while Workers handle URL generation and object retrieval.
Cost and Performance Benchmarks
Measured over one month with 10 k product downloads and 50 GB storage:
- Storage: $0.00018 per GB-month × 50 GB = $0.009 - Egress: 90 % cache hit → 1 k requests × $0.0004 = $0.40 - KV cache: 10 k reads × $0.000002 = $0.02 - Workers CPU: 1 ms per 64-byte header × 10 k = 10 seconds → $0.003 - Lightning fee ≈ 0.5 % of transaction value (typical for a $10 purchase = $0.05)
Total per 1 k downloads with $10 average purchase price: $