Skip to content

Latest commit

 

History

History
108 lines (87 loc) · 3.21 KB

how-to-use-minio-as-laravel-file-storage.md

File metadata and controls

108 lines (87 loc) · 3.21 KB

How to use Minio Server as Laravel Custom File Storage

Laravel has a customizable file storage system with ability to create custom drivers for it. In this recipe we will implement a custom file system driver to use Minio server for managing files.

1. Prerequisites

Install Minio Server from here.

2. Install Required Dependency for Laravel

Install league/flysystem package for aws-s3 : fork based on https://github.com/thephpleague/flysystem-aws-s3-v3

composer require coraxster/flysystem-aws-s3-v3-minio

3. Create Minio Storage ServiceProvider

Create MinioStorageServiceProvider.php file in app/Providers/ directory with this content:

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;

use Aws\S3\S3Client;
use League\Flysystem\AwsS3v3\AwsS3Adapter;
use League\Flysystem\Filesystem;
use Storage;

class MinioStorageServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap the application services.
     *
     * @return void
     */
    public function boot()
    {
      Storage::extend('minio', function ($app, $config) {
          $client = new S3Client([
              'credentials' => [
                  'key'    => $config["key"],
                  'secret' => $config["secret"]
              ],
              'region'      => $config["region"],
              'version'     => "latest",
              'bucket_endpoint' => false,
              'use_path_style_endpoint' => true,
              'endpoint'    => $config["endpoint"],
          ]);
          $options = [
              'override_visibility_on_copy' => true
          ];
          return new Filesystem(new AwsS3Adapter($client, $config["bucket"], '', $options));
      });
    }

    /**
     * Register the application services.
     *
     * @return void
     */
    public function register()
    {

    }
}

Register service provider by adding this line in config/app.php on providers section :

App\Providers\MinioStorageServiceProvider::class

Add config for minio in disks section of config/filesystems.php file :

  'disks' => [
    // other disks

    'minio' => [
        'driver' => 'minio',
        'key' => env('MINIO_KEY', 'your minio server key'),
        'secret' => env('MINIO_SECRET', 'your minio server secret'),
        'region' => 'us-east-1',
        'bucket' => env('MINIO_BUCKET','your minio bucket name'),
        'endpoint' => env('MINIO_ENDPOINT','http://localhost:9000')
    ]

  ]

Note : region is not required & can be set to anything.

4. Use Storage with Minio in Laravel

Now you can use disk method on storage facade to use minio driver :

Storage::disk('minio')->put('avatars/1', $fileContents);

Or you can set default cloud driver to minio in filesystems.php config file :

'cloud' => env('FILESYSTEM_CLOUD', 'minio'),

Sample Project

If you want, you could explore laravel-minio-sample project and the unit tests for understanding how to use Minio Server with Laravel