aarondfrancis/r2proxy
Composer 安装命令:
composer require aarondfrancis/r2proxy
包简介
A Laravel package for proxying Cloudflare R2 files with public path whitelisting
README 文档
README
Serve files from a private Cloudflare R2 bucket through your Laravel application.
This package proxies whitelisted paths from a private R2 bucket, allowing you to serve certain files publicly while keeping everything else secure. No need to make your bucket public or configure complex access policies—just define which path prefixes should be accessible and the package handles the rest with configurable caching headers.
Installation
composer require aarondfrancis/r2proxy
Publish the config file:
php artisan vendor:publish --tag=r2proxy-config
Configuration
If you don't already have an R2 disk configured, follow the Laravel S3 driver documentation to set one up.
Once you have an R2 disk, change the driver from s3 to r2_public:
// config/filesystems.php 'r2' => [ // Change from 's3' to 'r2_public' 'driver' => 'r2_public', // The route prefix for proxied files (e.g. /r2/images/photo.jpg) 'url' => '/r2/', // ... rest of your existing R2 config ],
Then configure which disks should be proxied in config/r2proxy.php:
use AaronFrancis\R2Proxy\PathValidator; return [ 'disks' => [ 'r2' => [ 'path_validator' => PathValidator::directories('images', 'videos'), ], ], ];
Multiple Disks
You can proxy multiple disks, each with their own path validator and cache settings:
'disks' => [ 'r2' => [ 'path_validator' => PathValidator::directories('images', 'videos'), ], 'r2-assets' => [ 'path_validator' => PathValidator::matches('css/*', 'js/*'), 'cache' => [ 'max_age' => 86400, // 1 day ], ], ],
Usage
The temporaryUrl method returns a proxy URL for public paths:
// Returns /r2/images/photo.jpg (proxied through your app) $url = Storage::disk('r2')->temporaryUrl('images/photo.jpg', now()->addHour()); // Private paths still get signed S3 URLs $url = Storage::disk('r2')->temporaryUrl('private/secret.pdf', now()->addHour());
Checking Path Access
use AaronFrancis\R2Proxy\Filesystem\R2PublicAdapter; if (R2PublicAdapter::isPathAllowed('images/photo.jpg', 'r2')) { // Path is publicly accessible on the 'r2' disk }
Path Validation Options
Directories - allow entire directories:
use AaronFrancis\R2Proxy\PathValidator; 'path_validator' => PathValidator::directories('images', 'uploads'),
Patterns - wildcard matching with *:
'path_validator' => PathValidator::matches('images/*.jpg', 'videos/*.mp4'),
Security
Only paths allowed by the path validator are accessible through the proxy. Requests to other paths return a 403 Forbidden response. Directory traversal attacks are blocked regardless of validator configuration.
How It Works
- Files in public paths are served through
/{url-prefix}/{path}routes - The controller streams files directly from R2 with proper headers
- Cache-Control headers are added for browser/CDN caching
- Private files still use signed S3 URLs via the parent adapter
License
MIT
aarondfrancis/r2proxy 适用场景与选型建议
aarondfrancis/r2proxy 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 757 次下载、GitHub Stars 达 4, 最近一次更新时间为 2026 年 01 月 17 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 aarondfrancis/r2proxy 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 aarondfrancis/r2proxy 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 757
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 4
- 点击次数: 29
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-01-17