Ref docs: https://docs.flarum.org/extend/filesystem/#storage-drivers
im trying to implement Cloudflare R2 as CDN.
im using league/flysystem-aws-s3-v3:^1.0.0
.
following this guide: https://developers.cloudflare.com/r2/examples/aws/aws-sdk-php/
I will start by sharing the code
extend.php
(new Extend\Filesystem)
->driver('r2', R2Driver::class) // applied to flarum-assets filesystem
R2Driver.php
<?php
namespace Nearata\Cloudflare\Filesystem;
use Aws\Credentials\Credentials;
use Aws\S3\S3Client;
use Flarum\Filesystem\DriverInterface;
use Flarum\Foundation\Config;
use Flarum\Settings\SettingsRepositoryInterface;
use Illuminate\Contracts\Filesystem\Cloud;
use Illuminate\Filesystem\FilesystemAdapter;
use League\Flysystem\Filesystem;
use Nearata\Cloudflare\Adapter\AwsS3Adapter;
class R2Driver implements DriverInterface
{
public function build(string $diskName, SettingsRepositoryInterface $settings, Config $config, array $localConfig): Cloud
{
$client = new S3Client([
'credentials' => [
'key' => $key,
'secret' => $secret
],
'region' => 'auto', // tried us-east-1 too
'endpoint' => $endpoint,
'version' => 'latest',
'use_path_style_endpoint' => true
]);
$adapter = new AwsS3Adapter($client, 'flarum');
return new FilesystemAdapter(new Filesystem($adapter));
}
}
AwsS3Adapter.php
<?php
namespace Nearata\Cloudflare\Adapter;
class AwsS3Adapter extends \League\Flysystem\AwsS3v3\AwsS3Adapter
{
public function getUrl(string $file): string
{
$baseUrl = "r2.dev / production";
if (empty($file)) {
return $baseUrl;
}
return "$baseUrl/$file";
}
}
Issues
- Im getting an empty string in
getUrl
, that's if not handled i get the exception reported in this pr: flarum/framework3701
- Flarum document response is very slow, like 10-15 seconds, but no exceptions in logs or console and the files are all 200OK in frontend when loaded.
i think im missing something, but im following the docs / tests very carefully but i cant get it.
edit 1: My bad, i was testing this with debug mode on lol