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.
Cara integrasi minio ke project Laravel

Cara menggunakan Minio Server sebagai Laravel 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.

Install projek Laravel here.

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

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 :

App\Providers\MinioStorageServiceProvider::class

Tambahkan konfigurasi di bagian disks pada file config/filesystems.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

Storage::disk('minio')->put('avatars/1', $fileContents);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment