Skip to content

Instantly share code, notes, and snippets.

@JacobBennett
Last active March 26, 2020 02:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JacobBennett/d64d08c5638e84d0dfafc38139726b51 to your computer and use it in GitHub Desktop.
Save JacobBennett/d64d08c5638e84d0dfafc38139726b51 to your computer and use it in GitHub Desktop.
Regarding Filesystems

Filesystem Decision Tree

Filesystem Driver

S3 BUCKET IN PRODUCTION

- How to use:
    - Use s3-swap driver and use minio for local development
    - Use Storage fake for tests

- when to use:
    - backups
    - uploaded files or documents storage (images, pdfs)
    - generated files or documents storage

MINIO IN PRODUCTION

- How to use:
    - Use s3 driver and use minio for local development
    - Use Storage fake for tests

- When to use:
    - Other internal, non-PHP processes need to be able to write to location
    - Convenient to access with Windows file explorer UI
    - Files will only be used internally

LOCAL IN PRODUCTION

- How to use:
    - use local driver
    - Use Storage fake for tests.

- When to use:
    - Files are present that we want in version control
    - Files that will change infrequently enough to justify being part of the repo
    - NON user uploaded files
    - Think "fixtures"

Filesystem Access Keys

Each application should have a single set of credentials per driver. The only exception to this would be if you were using multiple minio drives.

<?php
return [
'disks' => [
's3' => [
'driver' => 's3-swap',
'key' => env('AWS_ACCESS_KEY_ID'),
'secret' => env('AWS_SECRET_ACCESS_KEY'),
'region' => env('AWS_DEFAULT_REGION', 'us-east-1'),
'bucket' => env('AWS_BUCKET', 'wilberpay.com'),
'url' => env('AWS_URL'),
// local use
'endpoint' => env('MINIO_ENDPOINT'),
'use_path_style_endpoint' => true,
],
]
];
<?php
namespace App\Providers;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\ServiceProvider;
class SwapDriverServiceProvider extends ServiceProvider
{
/**
* Bootstrap services.
*
* @return void
*/
public function boot()
{
Storage::extend('s3-swap', function ($app, $config) {
return Storage::createS3Driver($this->getConfig($app, $config));
});
}
public function getConfig($app, $config)
{
return ! $app->environment('production') ?
$config :
Arr::except($config, [
'endpoint',
'use_path_style_endpoint',
]);
}
}
<?php
namespace App\Wilber\MediaLibrary\UrlGenerator;
use Illuminate\Contracts\Config\Repository as Config;
use Illuminate\Filesystem\FilesystemManager;
use Spatie\MediaLibrary\UrlGenerator\S3UrlGenerator;
class S3SwapUrlGenerator extends S3UrlGenerator
{
public function __construct(Config $config, FilesystemManager $filesystemManager)
{
parent::__construct($config, $filesystemManager);
}
public function getUrl(): string
{
if (! app()->environment('production')) {
// minio url
return config('medialibrary.minio.local.domain').'/'.$this->getPathRelativeToRoot();
}
return config('medialibrary.s3.domain').'/'.$this->getPathRelativeToRoot();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment