Laravel6でMinIOを使う
Laravelではドキュメント通りに必要なパッケージをインストールすればS3はすぐに使うことができるようになる。
ただ開発環境はMinIOで代用したい場合などは、少し工夫が必要となる。
というのもバケット指定方法が違うのだ。
ということで config/filesystems.php
にs3
とは別の設定を追加する。
config/filesystems.php
<?php return [ // ~ 'disks' => [ // ~ 'minio' => [ 'driver' => 's3', 'endpoint' => env('MINIO_ENDPOINT', 'http://127.0.0.1:9000'), 'use_path_style_endpoint' => true, 'key' => env('AWS_ACCESS_KEY_ID'), 'secret' => env('AWS_SECRET_ACCESS_KEY'), 'region' => env('AWS_DEFAULT_REGION'), 'bucket' => env('AWS_BUCKET'), ], ], // ~ ];
ここで重要なのが、endpoint
とuse_path_style_endpoint
である。
これを指定することでMinIOに対応してくれる。
ただしdiskの指定をminioにする必要があるため、ファサードを書き換えてしまったり、何か処理を噛ませるといいだろう。そのあたりは各々のプロジェクトで考えてほしい。
おまけ
参考までにファサード書き換えで本番環境と開発環境でs3の向き先を変える方法を記載しておく。
config/app.php
<?php return [ // ~ 'providers' => [ // ~ App\Filesystem\FilesystemServiceProvider::class, // ~ ], 'providers' => [ // ~ // 'Storage' => Illuminate\Support\Facades\Storage::class, 'Storage' => App\Filesystem\Storage::class, // ~ ], // ~ ];
app/Filesystem/FilesystemServiceProvider.php
<?php namespace App\Filesystem; use Illuminate\Support\ServiceProvider; class FilesystemServiceProvider extends ServiceProvider { /** * Register any application services. * * @return void */ public function register() { $this->app->bind('filesystem.expansion', function($app) { return new \App\Filesystem\FilesystemManager($app); }); } /** * Bootstrap any application services. * * @return void */ public function boot() { // } }
app/Filesystem/Storage.php
<?php namespace App\Filesystem; use Illuminate\Support\Facades\Storage as OrgStorage; /** * @method static \Illuminate\Contracts\Filesystem\Filesystem disk(string $name = null) * * @see \Illuminate\Filesystem\FilesystemManager */ class Storage extends OrgStorage { /** * Get the registered name of the component. * * @return string */ protected static function getFacadeAccessor() { return 'filesystem.expansion'; } }
app/Filesystem/FilesystemManager
<?php namespace App\Filesystem; use Illuminate\Filesystem\FilesystemManager as OrgFilesystemManager; /** * @method static \Illuminate\Contracts\Filesystem\Filesystem disk(string $name = null) * * @see \Illuminate\Filesystem\FilesystemManager */ class FilesystemManager extends OrgFilesystemManager { /** * Get a filesystem instance. * * @param string|null $name * @return \Illuminate\Contracts\Filesystem\Filesystem */ public function disk($name = null) { if (config('app.debug') && ($name === 's3')) { $name = 'minio'; } return parent::disk($name); } }