Skip to content

Instantly share code, notes, and snippets.

@gazben
Created March 20, 2020 17:09
Show Gist options
  • Select an option

  • Save gazben/869c505970c6098f6f8784374e52a1e9 to your computer and use it in GitHub Desktop.

Select an option

Save gazben/869c505970c6098f6f8784374e52a1e9 to your computer and use it in GitHub Desktop.
Laravel S3 multi region filesystem driver
<?php
namespace App\Providers;
use Illuminate\Support\Arr;
use Aws\S3\S3MultiRegionClient;
use League\Flysystem\Filesystem;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\ServiceProvider;
use League\Flysystem\AwsS3v3\AwsS3Adapter;
class S3MultiRegionServiceProvider extends ServiceProvider
{
/**
* Register services.
*
* @return void
*/
public function register()
{
//
}
/**
* Bootstrap services.
*
* @return void
*/
public function boot()
{
Storage::extend('s3multiregion', function ($app, $config) {
$s3Config = $this->formatS3Config($config);
$root = $s3Config['root'] ?? null;
$options = $config['options'] ?? [];
$client = new S3MultiRegionClient($s3Config);
$adapter = new AwsS3Adapter($client, $s3Config['bucket'], $root, $options);
return new Filesystem($adapter, $config);
});
}
protected function formatS3Config(array $config)
{
$config += ['version' => 'latest'];
if (! empty($config['key']) && ! empty($config['secret'])) {
$config['credentials'] = Arr::only($config, ['key', 'secret', 'token']);
}
return $config;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment