Laravel Storage

Multiple S3 Buckets

Add one more “disk” config in filesystems.php:

's3_upload' => [
	'driver' => 's3',
	'key' => env('AWS_ACCESS_KEY_ID'),
	'secret' => env('AWS_SECRET_ACCESS_KEY'),
	'region' => env('AWS_DEFAULT_REGION'),
	'bucket' => env('AWS_UPLOAD_BUCKET'),
's3' => [
	'driver' => 's3',
	'key' => env('AWS_ACCESS_KEY_ID'),
	'secret' => env('AWS_SECRET_ACCESS_KEY'),
	'region' => env('AWS_DEFAULT_REGION'),
	'bucket' => env('AWS_BUCKET'),
	'url' => env('AWS_URL'),
	'endpoint' => env('AWS_ENDPOINT'),
],

's3_upload' => [
	'driver' => 's3',
	'key' => env('AWS_ACCESS_KEY_ID'),
	'secret' => env('AWS_SECRET_ACCESS_KEY'),
	'region' => env('AWS_DEFAULT_REGION'),
	'bucket' => env('AWS_UPLOAD_BUCKET'),
	'url' => env('AWS_URL'),
	'endpoint' => env('AWS_ENDPOINT'),
],

Usage:

use Illuminate\Support\Facades\Storage;

Storage::disk('s3')->putFileAs($dest, $file, $fileName, 'public');
Storage::disk('s3_upload')->putFileAs($dest, $file, $fileName, 'public');

Get correct MIME type:

// Origin name: video1234.mp4
$file = $req->file('video');

// Result: bin
logg($file->extension());

// Result mp4
logg($file->getClientOriginalExtension());

Common methods

$path = Storage::putFileAs('public/' . $dest, $file, $fileName);
$local_path = Storage::path($path);
$size = Storage::size($path);

Reference:

  1. https://laracasts.com/discuss/channels/requests/putfile-method-stores-mp3-files-as-bin-in-laravel?page=0
  2. https://laracasts.com/discuss/channels/laravel/laravel-54-uploadedfilegetmimetype-returns-incorrect-mimetype