<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Fred Shema</title>
    <description>The latest articles on DEV Community by Fred Shema (@shema).</description>
    <link>https://dev.to/shema</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F454383%2F447e9360-2ccd-4b82-943c-bd209ecc95c0.jpg</url>
      <title>DEV Community: Fred Shema</title>
      <link>https://dev.to/shema</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/shema"/>
    <language>en</language>
    <item>
      <title>PHP object storage using MinIO deployed on fly.io</title>
      <dc:creator>Fred Shema</dc:creator>
      <pubDate>Mon, 16 Jan 2023 20:52:29 +0000</pubDate>
      <link>https://dev.to/shema/php-object-storage-using-minio-deployed-on-flyio-121e</link>
      <guid>https://dev.to/shema/php-object-storage-using-minio-deployed-on-flyio-121e</guid>
      <description>&lt;p&gt;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. &lt;a href="https://min.io/" rel="noopener noreferrer"&gt;MinIO&lt;/a&gt; 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 &lt;a href="https://fly.io/" rel="noopener noreferrer"&gt;fly.io&lt;/a&gt; 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.&lt;/p&gt;

&lt;h2&gt;
  
  
  Deploying MinIO on fly.io
&lt;/h2&gt;

&lt;p&gt;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 &lt;a href="https://fly.io/docs/hands-on/sign-up/" rel="noopener noreferrer"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Once you have created an account, you can refer to this awesome &lt;a href="https://fly.io/docs/app-guides/minio/" rel="noopener noreferrer"&gt;guide&lt;/a&gt; on their documentation to deploy a MinIO instance.&lt;/p&gt;

&lt;h2&gt;
  
  
  Connecting to MinIO admin console
&lt;/h2&gt;

&lt;p&gt;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:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;flyctl proxy 9001:9001 &lt;span class="nt"&gt;-a&lt;/span&gt; &amp;lt;app-name&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can now access the admin console on &lt;a href="http://localhost:9001" rel="noopener noreferrer"&gt;http://localhost:9001&lt;/a&gt;. You can login with the credentials you provided when you deployed MinIO. Now you can create buckets and manage your files.&lt;/p&gt;

&lt;h2&gt;
  
  
  Using MinIO with PHP
&lt;/h2&gt;

&lt;p&gt;To use MinIO with PHP, you will need to install the &lt;code&gt;league/flysystem-aws-s3-v3&lt;/code&gt; package. You can install it with composer by running the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;composer require league/flysystem-aws-s3-v3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Create a bucket on MinIO and add the following environment variables to your &lt;code&gt;.env&lt;/code&gt; file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;MINIO_BUCKET=&amp;lt;bucket-name&amp;gt;
MINIO_ENDPOINT=https://&amp;lt;app-name&amp;gt;.fly.dev
MINIO_KEY=admin
MINIO_SECRET=&amp;lt;minio-password&amp;gt;
MINIO_REGION=us-east-1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In your &lt;code&gt;config/filesystems.php&lt;/code&gt; file, add the following configuration in the &lt;code&gt;disks&lt;/code&gt; array:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="s1"&gt;'minio'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
            &lt;span class="s1"&gt;'driver'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'s3'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="s1"&gt;'endpoint'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;env&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'MINIO_ENDPOINT'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'http://127.0.0.1:9000'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
            &lt;span class="s1"&gt;'use_path_style_endpoint'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="s1"&gt;'key'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;env&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'MINIO_KEY'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
            &lt;span class="s1"&gt;'secret'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;env&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'MINIO_SECRET'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
            &lt;span class="s1"&gt;'region'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;env&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'MINIO_REGION'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
            &lt;span class="s1"&gt;'bucket'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;env&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'MINIO_BUCKET'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
        &lt;span class="p"&gt;],&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can now store files on MinIO by using the &lt;code&gt;minio&lt;/code&gt; disk. You can also use the &lt;code&gt;Storage&lt;/code&gt; facade to store files on MinIO. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nc"&gt;Storage&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;disk&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'minio'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;put&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'file.txt'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'Hello World'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;I hope this guide was helpful. If you have any questions, feel free to ask them in the comments.&lt;/p&gt;

&lt;h2&gt;
  
  
  Resources
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://fly.io/docs/app-guides/minio/" rel="noopener noreferrer"&gt;Deploy MinIO Object Storage to Fly.io&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://laravel.com/docs/9.x/filesystem#amazon-s3-compatible-filesystems" rel="noopener noreferrer"&gt;Laravel - Amazon S3 Compatible Filesystems&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://laravel-news.com/minio-s3-compliant-storage" rel="noopener noreferrer"&gt;MinIO: An Open-Source S3 Compliant Storage Service&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>career</category>
      <category>productivity</category>
      <category>fullstack</category>
      <category>softwaredevelopment</category>
    </item>
  </channel>
</rss>
