Skip to content

Instantly share code, notes, and snippets.

@widada
Last active November 29, 2024 08:53
Show Gist options
  • Save widada/4188f03cc8056aa393a46bc6e55c3f18 to your computer and use it in GitHub Desktop.
Save widada/4188f03cc8056aa393a46bc6e55c3f18 to your computer and use it in GitHub Desktop.

Revisions

  1. widada revised this gist Dec 18, 2022. 2 changed files with 123 additions and 1 deletion.
    123 changes: 123 additions & 0 deletions LaravelMinio.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,123 @@
    # Cara menggunakan Minio Server sebagai [Laravel](https://laravel.com) Custom File Storage

    `Laravel` mempunyai konfigurasi file storage yang sangat customizable, Jadi kita bisa mengubah drivernya sesuai kebutuhan. Pada tutorial kali ini kita akan mengimplementasi Minio server untuk memanage file-file di aplikasi kita.

    ## 1. Prerequisites

    Install Minio Server [here](https://www.minio.io/downloads.html).

    Install projek Laravel [here](https://laravel.com/docs/9.x/installation).

    Buat variable environment di file `.env` dan sesuaikan dengan credentials Minio kalian.
    ```
    MINIO_KEY=
    MINIO_SECRET=
    MINIO_REGION=us-east-1
    MINIO_ENDPOINT=
    MINIO_BUCKET=
    MINIO_VERSION=latest
    ```

    ## 2. Install Dependency yang dibutuhkan.

    Install `aws/aws-sdk-php`

    ```
    composer require aws/aws-sdk-php
    ```

    Install `league/flysystem` package
    ```
    composer require coraxster/flysystem-aws-s3-v3-minio
    ```


    ## 3. Create Minio Storage ServiceProvider
    Jalankan perintah berikut untuk membuat service provider
    ```
    php artisan make:provider MinioStorageServiceProvider
    ```
    Kemudian buka file tersebut di folder `app/Providers/`, selanjutnya sesuaikan isinya seperti berikut ini:

    ```php
    <?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
    {
    /**
    * Register services.
    *
    * @return void
    */
    public function register()
    {
    //
    }

    /**
    * Bootstrap 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));
    });
    }
    }
    ```

    Daftarkan service provider tersebut di file `config/app.php` pada bagian `providers` :

    ```php
    App\Providers\MinioStorageServiceProvider::class
    ```

    Tambahkan konfigurasi di bagian `disks` pada file `config/filesystems.php` :

    ```php
    'disks' => [
    // other disks

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

    ]
    ```
    Note : value `region` bersifat opsional dan bisa kamu set apapun.

    ## 4. Penggunaan Minio di Storage Laravel
    ```php
    Storage::disk('minio')->put('avatars/1', $fileContents);
    ```
    1 change: 0 additions & 1 deletion laravel_minio.md
    Original file line number Diff line number Diff line change
    @@ -1 +0,0 @@
    ## test
  2. widada renamed this gist Dec 17, 2022. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. widada created this gist Dec 17, 2022.
    1 change: 1 addition & 0 deletions Cara Integrasi Minio di Laravel
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    ## test