Created
March 20, 2020 17:09
-
-
Save gazben/869c505970c6098f6f8784374e52a1e9 to your computer and use it in GitHub Desktop.
Laravel S3 multi region filesystem driver
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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