PHP object storage using MinIO deployed on fly.io
Tags: php, webdev, laravel, programming
Published: 2023-01-16 20:52:29

Through my experience with PHP, I have found that files are a very important part of the web. But for the most part, I rarely get to deal with them because many systems I worked on didn't necessarily need them. But in the past months I found myself working on a project that required me to store files. I had to find a way to store them in a way that was easy to manage and scale. PHP has a very robust filesystem that makes it easy to deal with any type of files. But in the middle of the development process I quickly realized that the platform we were supposed to deploy to had an ephemeral filesystem. This meant that any files that were stored on the filesystem would be lost when the server was restarted either triggered by a deployment or a crash. I had to find another solution that would allow me to store files in a way that would be persistent. Amazon's S3 bucket was the first choice because it's filesystem is supported by PHP, but it was not an option because of the cost. I had to find a cheaper alternative and that's how I stumbled upon MinIO. [MinIO](https://min.io/) is an open source object storage server that is compatible with Amazon's S3 bucket. It's free and open source and it's a great alternative to Amazon's S3 bucket. I decided to deploy MinIO on [fly.io](https://fly.io/) because it's a great platform for deploying applications and it's free for small applications. I will be showing you how to deploy MinIO on fly.io and how to use it with PHP.



## Deploying MinIO on fly.io

To deploy MinIO on fly.io, you will need to have a fly account. If you don't have one, you can learn how to create one [here](https://fly.io/docs/hands-on/sign-up/)

Once you have created an account, you can refer to this awesome [guide](https://fly.io/docs/app-guides/minio/) on their documentation to deploy a MinIO instance.



## Connecting to MinIO admin console

The MinIO admin console is a web interface that allows you to manage your MinIO instance. To access the admin console, you will need to proxy your instance to your local machine on port 9001. To do this, you will need to run the following command:

```bash
flyctl proxy 9001:9001 -a <app-name>
```

You can now access the admin console on [http://localhost:9001](http://localhost:9001). You can login with the credentials you provided when you deployed MinIO. Now you can create buckets and manage your files.



## Using MinIO with PHP

To use MinIO with PHP, you will need to install the `league/flysystem-aws-s3-v3` package. You can install it with composer by running the following command:

```bash
composer require league/flysystem-aws-s3-v3
```

Create a bucket on MinIO and add the following environment variables to your `.env` file: 

```text
MINIO_BUCKET=<bucket-name>
MINIO_ENDPOINT=https://<app-name>.fly.dev
MINIO_KEY=admin
MINIO_SECRET=<minio-password>
MINIO_REGION=us-east-1
```

In your `config/filesystems.php` file, add the following configuration in the `disks` array:

```php
'minio' => [
            'driver' => 's3',
            'endpoint' => env('MINIO_ENDPOINT', 'http://127.0.0.1:9000'),
            'use_path_style_endpoint' => true,
            'key' => env('MINIO_KEY'),
            'secret' => env('MINIO_SECRET'),
            'region' => env('MINIO_REGION'),
            'bucket' => env('MINIO_BUCKET'),
        ],
```

You can now store files on MinIO by using the `minio` disk. You can also use the `Storage` facade to store files on MinIO. For example:

```php
Storage::disk('minio')->put('file.txt', 'Hello World');
```

If you want to use the `Storage` facade as your default disk, you can set the `default` key in the `config/filesystems.php` file to `minio` or add `FILESYSTEM_DISK=minio` to your `.env` file.

## Conclusion

I hope this guide was helpful. If you have any questions, feel free to ask them in the comments.

## Resources

- [Deploy MinIO Object Storage to Fly.io](https://fly.io/docs/app-guides/minio/)
- [Laravel - Amazon S3 Compatible Filesystems](https://laravel.com/docs/9.x/filesystem#amazon-s3-compatible-filesystems)
- [MinIO: An Open-Source S3 Compliant Storage Service](https://laravel-news.com/minio-s3-compliant-storage)