PHP code example of aarondfrancis / r2proxy

1. Go to this page and download the library: Download aarondfrancis/r2proxy library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

aarondfrancis / r2proxy example snippets


// 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
],

use AaronFrancis\R2Proxy\PathValidator;

return [
    'disks' => [
        'r2' => [
            'path_validator' => PathValidator::directories('images', 'videos'),
        ],
    ],
];

'disks' => [
    'r2' => [
        'path_validator' => PathValidator::directories('images', 'videos'),
    ],
    'r2-assets' => [
        'path_validator' => PathValidator::matches('css/*', 'js/*'),
        'cache' => [
            'max_age' => 86400, // 1 day
        ],
    ],
],

// 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());

use AaronFrancis\R2Proxy\Filesystem\R2PublicAdapter;

if (R2PublicAdapter::isPathAllowed('images/photo.jpg', 'r2')) {
    // Path is publicly accessible on the 'r2' disk
}

use AaronFrancis\R2Proxy\PathValidator;

'path_validator' => PathValidator::directories('images', 'uploads'),

'path_validator' => PathValidator::matches('images/*.jpg', 'videos/*.mp4'),
bash
php artisan vendor:publish --tag=r2proxy-config