Storage
Upload images, audio, and PDF files to Cloudflare R2.
Cloudflare R2 is used to store image, audio, and PDF files. It is free, has zero egress fees, and is S3 compatible.
You need to set up Cloudflare storage if you are using any of the following demo apps:
Chat with PDF
Upload a PDF and use OpenAI to chat with it
Voice to notes
Record audio, transcribe it and summarize it using AI
Stable Diffusion XL
Generate images using SDXL
DALL-E
Generate logos using DALL-E
GPT-4o & Vision
Analyze and describe images using GPT-4 or GPT Vision
API routes are set up for all the demo apps. For more specific information, check out their respective documentation pages to find the API routes.
Set up Cloudflare R2
Setting it up is easy and shouldn’t take more than 5 minutes.
1. Create a bucket
Create an R2 bucket on Cloudflare
- Go to Cloudflare -> R2 -> Overview
- Click “Create a bucket”
- Give your bucket a name
- Choose a location
Your bucket is ready! Now we need to give it a publicly accessible URL and get the required API keys to add to our .env
file.
2. Enable public access
- Go to your bucket
- Click on Settings
- Copy your S3 API and Location
- You can remove the name of your bucket from the link. For instance, in the image below, you would copy everything except the /testing part.
Copy your endpoint ID and location
- Now you have two options:
- Option A: If you have a domain name on Cloudflare, add a custom domain, e.g.,
cdn.anotherwrapper.com
. Your files will be accessible from that domain. - Option B: If you don’t have a custom domain, allow the
R2.dev
subdomain. Note: This is not recommended for production environments as it is rate-limited.
- Option A: If you have a domain name on Cloudflare, add a custom domain, e.g.,
Whatever option you choose, copy this URL as we’ll need it for our .env
file.
Use an R2.dev domain if you don't have a custom one
3. Get API keys and other information
- Go to R2 -> Overview
- Click on Manage R2 API tokens
Click on Manage R2 API tokens
- Click on Create API token
- Under permissions, give it
Admin Read & Write
. This allows users to delete their own data. - Define how long the token will stay active
- Click save
You should now have your API tokens. Copy the Access Key ID and the Secret Access Key.
Copy the Access Key ID and the Secret Access Key
Add the following to your .env
file:
That’s it - you should now be able to use uplaoding functionalities in the app.
If you want to check it out, the S3 client is set up and stored under @/lib/cloudflare.ts
.
Use alternatives to Cloudflare R2
Cloudflare R2 is S3 compatible and uses the same API, so you should be able to fairly easily use other S3 compatible storage providers.
I’m still working on adding a documentation for how to set it up, for example, on AWS S3.