Add Cloudflare R2 storage to Laravel in 5 minutes | Medium
Extracto
Let's see how to configure a Cloudflare R2 storage disk on Laravel projects, a cost-effective alternative to AWS S3, to reduce your infrastructure costs.
Contenido
Cloudflare R2 is an S3-compatible object storage solution, which could be an excellent alternative to AWS S3. R2 is cheaper; using it instead of AWS S3 can save you costs (75% savings in my case).
Let’s see how to create a Laravel filesystem disk that uses Cloudflare R2.
1. Install the S3 filesystem driver
composer require league/flysystem-aws-s3-v3 "^3.0"2. Create a new disk configuration
Update the config/filesystems.php file to add a new r2 disk.
<?phpreturn [
// ...
'disks' => [
// ...
'r2' => [
'driver' => 's3',
'key' => env('CLOUDFLARE_R2_ACCESS_KEY_ID'),
'secret' => env('CLOUDFLARE_R2_SECRET_ACCESS_KEY'),
'region' => 'us-east-1',
'bucket' => env('CLOUDFLARE_R2_BUCKET'),
'url' => env('CLOUDFLARE_R2_URL'),
'visibility' => 'private',
'endpoint' => env('CLOUDFLARE_R2_ENDPOINT'),
'use_path_style_endpoint' => env('CLOUDFLARE_R2_USE_PATH_STYLE_ENDPOINT', false),
'throw' => false,
],
],
];
Don’t mind the region; just put us-east-1 even though there are no R2 regions with Cloudflare.
Add the environment variables to your .env.example.
CLOUDFLARE_R2_ACCESS_KEY_ID=
CLOUDFLARE_R2_SECRET_ACCESS_KEY=
CLOUDFLARE_R2_BUCKET=
CLOUDFLARE_R2_ENDPOINT=
CLOUDFLARE_R2_URL=3. Create a new R2 bucket
Go to your Cloudflare dashboard, choose “R2” from the sidebar and click on the “Create bucket” button.
We’re going to name it laradoc-storage for this example.
4. Generate an API token
From the R2 page, click on “Manage R2 API Tokens”.
And then, create a new token with the “Edit” permission:
Once created, you get an Access Key ID and a Secret Access Key. Save them somewhere! 🔑
5. Update your environment file
Get your CLOUDFLARE_R2_ENDPOINT on your bucket page.
Then, fill your .env file like so:
CLOUDFLARE_R2_ACCESS_KEY_ID=YOUR_ACCESS_KEY
CLOUDFLARE_R2_SECRET_ACCESS_KEY=YOUR_SECRET_KEY
CLOUDFLARE_R2_BUCKET=YOUR_BUCKET_NAME
CLOUDFLARE_R2_ENDPOINT=XXXXXXX.r2.cloudflarestorage.com
CLOUDFLARE_R2_URL=That’s it! You can now use your R2 disk like any other disk.
Let’s see a few examples from the Laravel documentation, but applied to our new R2 disk.
use Illuminate\Support\Facades\Storage;// Retrieving Files
Storage::disk('r2')->get('file.jpg');
// Storing Files
Storage::disk('r2')->put('example.txt', 'Hello World');
// Check if a file exists
Storage::disk('r2')->exists('file.jpg')
// Moving Files
Storage::disk('r2')->copy('old/file.jpg', 'new/file.jpg');
// Copying Files
Storage::disk('r2')->move('old/file.jpg', 'new/file.jpg');
<?phpnamespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class UserAvatarController extends Controller
{
/**
* Update the avatar for the user.
*/
public function update(Request $request)
{
$path = $request->file('avatar')->store('avatars', 'r2');
$request->user()->update([
'avatar_path' => $path,
]);
return back();
}
}
You may also set r2 as your default disk and you won’t even need to specify disk('r2') in all these examples.
If you want that, update the FILESYSTEM_DISK variable in your .env file.
FILESYSTEM_DISK=r2Fuente: Medium