FILE STORAGE / LESSON 03
Securing and Setting Up R2.
Public and private files, setting up a bucket, and R2 pricing

Loading the lesson…
Loading activities…
FILE STORAGE / LESSON 03
Public and private files, setting up a bucket, and R2 pricing

Loading the lesson…
Loading activities…
Not every file should be public. Think about what you're storing.
These can have public URLs that anyone can open.
These should require someone to be signed in. R2 supports "signed URLs," which are temporary links that expire after a set time. In this snippet, client is the S3 client you'll create in the next lesson, and the GetObjectCommand says which file to share.
getSignedUrl lives in its own package, so install it with npm install @aws-sdk/s3-request-presigner next to @aws-sdk/client-s3.
Only signed-in users can request a signed URL, and it only works for a limited time. Somebody forwards the link next week? Dead link.
Here's the basic process to get R2 up and running.
Sign up at cloudflare.com. R2 comes with free monthly usage (more on that in Pricing below).
In the Cloudflare dashboard, go to Storage & databases, then R2, and go through the checkout to add an R2 subscription. Yep, even the free usage needs that step, so don't be surprised if it asks for billing info. You don't pay anything until you go past the free amounts.
A bucket is where your files live. Give it a name that actually describes what's in it (future you will thank you).
On the R2 page, find API Tokens under Account Details and click Manage. Create a token with only the permissions your app needs, and you'll get an Access Key ID and a Secret Access Key. Copy the secret RIGHT AWAY, because Cloudflare only shows it once.
Use the S3 SDK in your code to upload and grab files.
R2 pricing is simple and pretty cheap. Here's the standard storage pricing as of October 2026 (always double check the R2 pricing page before you promise a client anything).
For context, a small app storing 10 GB of images fits inside the free tier, so it costs $0. Even at 100 GB you'd pay for the 90 GB over the free amount, which works out to about $1.35 a month. That's thousands and thousands of images for less than a fancy coffee.