DEV Community

Fred Shema
Fred Shema

Posted on

PHP object storage using MinIO deployed on fly.io

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 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 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

Once you have created an account, you can refer to this awesome guide 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:

flyctl proxy 9001:9001 -a <app-name>
Enter fullscreen mode Exit fullscreen mode

You can now access the admin console on 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:

composer require league/flysystem-aws-s3-v3
Enter fullscreen mode Exit fullscreen mode

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

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

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

'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'),
        ],
Enter fullscreen mode Exit fullscreen mode

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:

Storage::disk('minio')->put('file.txt', 'Hello World');
Enter fullscreen mode Exit fullscreen mode

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

Top comments (1)

Collapse
 
adamthedeveloper profile image
Adam

Thank you for this!