Skip to content

Instantly share code, notes, and snippets.

@JacobBennett
Last active March 26, 2020 02:43
Show Gist options
  • Select an option

  • Save JacobBennett/d64d08c5638e84d0dfafc38139726b51 to your computer and use it in GitHub Desktop.

Select an option

Save JacobBennett/d64d08c5638e84d0dfafc38139726b51 to your computer and use it in GitHub Desktop.

Revisions

  1. JacobBennett renamed this gist Mar 26, 2020. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. JacobBennett revised this gist Mar 26, 2020. 1 changed file with 25 additions and 0 deletions.
    25 changes: 25 additions & 0 deletions S3SwapUrlGenerator.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,25 @@
    <?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();
    }
    }
  3. JacobBennett revised this gist Mar 26, 2020. 1 changed file with 32 additions and 0 deletions.
    32 changes: 32 additions & 0 deletions s3SwapServiceProvider.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,32 @@
    <?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',
    ]);
    }
    }
  4. JacobBennett revised this gist Mar 26, 2020. 1 changed file with 18 additions and 0 deletions.
    18 changes: 18 additions & 0 deletions filesystems.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,18 @@
    <?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,
    ],
    ]
    ];
  5. JacobBennett created this gist Mar 26, 2020.
    38 changes: 38 additions & 0 deletions Filesysystems-decision-tree.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,38 @@
    # 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.