Member-only story
Integrating Cloudflare R2 with Laravel: A Step-by-Step Guide
How to Store and Serve Files in Laravel Using Cloudflare R2
If you’re running a Laravel project and need affordable object storage, Cloudflare R2 is a great option. It works similarly to AWS S3 but without egress fees, which makes it ideal for projects that need to serve a lot of data. The only catch? Laravel doesn’t support R2 out of the box, so you’ll need to configure it manually.
This guide walks you through setting up Cloudflare R2 with Laravel. We’ll cover everything from creating an R2 bucket to configuring Laravel so you can store and retrieve files with ease.
Step 1: Set Up Cloudflare R2
Before Laravel can store files in Cloudflare R2, you need an R2 bucket and API credentials. Here’s how to get started:
- Log into Cloudflare and navigate to the R2 section.
- Create a new bucket and give it a unique name.
- Generate API credentials:
- Go to ‘API Tokens’ in your Cloudflare dashboard.
- Create a new token with permissions to read and write to R2.
- Copy the Access Key ID and Secret Access Key somewhere safe. You’ll need them in Laravel.
