Minio: An Open-Source S3 Compliant Storage Service

Published on by

Minio: An Open-Source S3 Compliant Storage Service image

Have you ever wanted a local version of Amazon S3 while developing Laravel applications? Well want no more, Minio is an open-source distributed object storage server built in Golang. The best part: Minio is Amazon S3 compatible. Let’s go through setting up Minio locally and then try out the new temporaryUrl method introduced in Laravel v5.4.31.

Installing Minio

According to the the quick start guide, Minio is described as:

…an object storage server released under Apache License v2.0. It is compatible with Amazon S3 cloud storage service. It is best suited for storing unstructured data such as photos, videos, log files, backups and container / VM images. Size of an object can range from a few KBs to a maximum of 5TB.

You can install Minio in a couple of different ways, including a Docker image and installing a binary on your machine. The quick start guide contains binaries you can download for your platform of choice.

If you are on OS X, you can install Minio with Homebrew:

brew install minio/stable/minio

Once you have Minio installed you can run it like this:

mkdir /tmp/minio
minio server /tmp/minio

I created the folder Minio will use in /tmp, but you could create this anywhere you want.

Minio’s default port is 9000, which means if you are running PHP-FPM on your machine you will need to specify a different port like so:

minio server --address :9005 /tmp/minio
 
Endpoint: http://192.168.1.9:9005 http://127.0.0.1:9005
AccessKey: KBSIYRR36U3A1IO1QARI
SecretKey: Z9BV6YsP7jtRQR1qCJk3PWecs22smNTOl7HC1Yj3
 
Browser Access:
http://192.168.1.9:9005 http://127.0.0.1:9005
 
# ...

You can now access a control panel in your browser with the provided URL using the key and secret credentials to login. With a local instance of Minio running, let’s take it for a test drive with Laravel.

Laravel Minio Setup

Because Minio is S3 compliant, you can use it with Laravel’s S3 driver, with a couple of tweaks. Assuming you have installed the S3 driver as outlined in the filesystem documentation, add a new configuration key in config/filesystems.php:

<?php
 
return [
// ...
'cloud' => env('FILESYSTEM_CLOUD', 's3'),
 
'disks' => [
 
// ...
 
'minio' => [
'driver' => 's3',
'endpoint' => env('MINIO_ENDPOINT', 'http://127.0.0.1:9005'),
'use_path_style_endpoint' => true,
'key' => env('AWS_KEY'),
'secret' => env('AWS_SECRET'),
'region' => env('AWS_REGION'),
'bucket' => env('AWS_BUCKET'),
],
],
];

Update your local .env file to use the minio configuration locally:

# .env file
 
# Default cloud will be Minio locally
FILESYSTEM_CLOUD=minio
 
# Minio config
MINIO_ENDPOINT="http://127.0.0.1:9005"
AWS_KEY=KBSIYRR36U3A1IO1QARI
AWS_SECRET=Z9BV6YsP7jtRQR1qCJk3PWecs22smNTOl7HC1Yj3
AWS_REGION=us-east-1
AWS_BUCKET=test

Before you can use this, you need to create the “test” bucket to match your configuration. Login to Minio at http://127.0.0.1:9005 and click the “plus” button on the bottom right. Based on our .env configuration, create a bucket named “test”.

If you want the files in the “test” bucket to have read-only access, you need to create a policy by clicking the bucket options icon in the left navigation menu and click “add” in the modal to allow read-only to all files in the bucket.

You should now be ready to start using Minio with Laravel as the default cloud driver locally!

Creating Files with Laravel

Now that Minio and Laravel are set up, let’s test things out in tinker:

php artisan tinker
 
>>> Storage::cloud()->put('hello.json', '{"hello": "world"}');
=> true
>>> Storage::cloud()->get('hello.json');
=> "{"hello": "world"}"
>>> file_get_contents('http://localhost:9005/test/hello.json');
=> "{"hello": "world"}"

Getting the default cloud driver with Storage::cloud(), our code can use Minio locally and Amazon S3 in production. We demonstrate getting the file from Minio through the S3 driver and through an HTTP request via file_get_contents().

This week, Mohamed Said added a temporaryUrl() method that works exclusively with the S3 driver in Laravel. Now that you have a file stored in Minio, let’s create a temporary URL (you must be using >= Laravel 5.4.31) just like we can in S3:

>>> Storage::cloud()->temporaryUrl("hello.json", \Carbon\Carbon::now()->addMinutes(1));
=> "http://127.0.0.1:9005/test/hello.json?X-Amz-Content-Sha256=UNSIGNED-PAYLOAD&X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=KBSIYRR36U3A1IO1QARI%2F20170803%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20170803T034835Z&X-Amz-SignedHeaders=host&X-Amz-Expires=60&X-Amz-Signature=b58451a860d2a511d703d60fcd425edb3dfac3c8c5f0adb1f3b67bcf3d9cfae1"

If you open the URL returned from temporaryUrl() you will get the JSON response we originally created. If you wait 1 minute, you will eventually get a 403 response.

<Error>
<Code>AccessDenied</Code>
<Message>Request has expired</Message>
<Key/>
<BucketName/>
<Resource>/test/hello.json</Resource>
<RequestId>3L137</RequestId>
<HostId>3L137</HostId>
</Error>

Now you have a free, open-source, lightweight, S3-compatible storage service you can run locally! All your code in existing projects that use S3 should work transparently with Minio.

Conclusion

I love isolating my development environment locally as much as possible and mimicking production at the same time. Minio is a great alternative to running S3 in development and testing against the S3 API. It’s also lightweight enough that you can run it in your stack. Check out minio.io to learn more.

Paul Redmond photo

Staff writer at Laravel News. Full stack web developer and author.

Filed in:
Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.

Laravel Forge logo

Laravel Forge

Easily create and manage your servers and deploy your Laravel applications in seconds.

Laravel Forge
Tinkerwell logo

Tinkerwell

The must-have code runner for Laravel developers. Tinker with AI, autocompletion and instant feedback on local and production environments.

Tinkerwell
No Compromises logo

No Compromises

Joel and Aaron, the two seasoned devs from the No Compromises podcast, are now available to hire for your Laravel project. ⬧ Flat rate of $7500/mo. ⬧ No lengthy sales process. ⬧ No contracts. ⬧ 100% money back guarantee.

No Compromises
Kirschbaum logo

Kirschbaum

Providing innovation and stability to ensure your web application succeeds.

Kirschbaum
Shift logo

Shift

Running an old Laravel version? Instant, automated Laravel upgrades and code modernization to keep your applications fresh.

Shift
Bacancy logo

Bacancy

Supercharge your project with a seasoned Laravel developer with 4-6 years of experience for just $2500/month. Get 160 hours of dedicated expertise & a risk-free 15-day trial. Schedule a call now!

Bacancy
Lucky Media logo

Lucky Media

Get Lucky Now - the ideal choice for Laravel Development, with over a decade of experience!

Lucky Media
Lunar: Laravel E-Commerce logo

Lunar: Laravel E-Commerce

E-Commerce for Laravel. An open-source package that brings the power of modern headless e-commerce functionality to Laravel.

Lunar: Laravel E-Commerce
LaraJobs logo

LaraJobs

The official Laravel job board

LaraJobs
SaaSykit: Laravel SaaS Starter Kit logo

SaaSykit: Laravel SaaS Starter Kit

SaaSykit is a Multi-tenant Laravel SaaS Starter Kit that comes with all features required to run a modern SaaS. Payments, Beautiful Checkout, Admin Panel, User dashboard, Auth, Ready Components, Stats, Blog, Docs and more.

SaaSykit: Laravel SaaS Starter Kit
Supercharge Your SaaS Development with FilamentFlow: The Ultimate Laravel Filament Boilerplate logo

Supercharge Your SaaS Development with FilamentFlow: The Ultimate Laravel Filament Boilerplate

Build your SaaS application in hours. Out-of-the-box multi-tenancy and seamless Stripe integration. Supports subscriptions and one-time purchases, allowing you to focus on building and creating without repetitive setup tasks.

Supercharge Your SaaS Development with FilamentFlow: The Ultimate Laravel Filament Boilerplate
Rector logo

Rector

Your partner for seamless Laravel upgrades, cutting costs, and accelerating innovation for successful companies

Rector
MongoDB logo

MongoDB

Enhance your PHP applications with the powerful integration of MongoDB and Laravel, empowering developers to build applications with ease and efficiency. Support transactional, search, analytics and mobile use cases while using the familiar Eloquent APIs. Discover how MongoDB's flexible, modern database can transform your Laravel applications.

MongoDB

The latest

View all →
Asymmetric Property Visibility in PHP 8.4 image

Asymmetric Property Visibility in PHP 8.4

Read article
Access Laravel Pulse Data as a JSON API image

Access Laravel Pulse Data as a JSON API

Read article
Laravel Forge adds Statamic Integration image

Laravel Forge adds Statamic Integration

Read article
Transform Data into Type-safe DTOs with this PHP Package image

Transform Data into Type-safe DTOs with this PHP Package

Read article
PHPxWorld - The resurgence of PHP meet-ups with Chris Morrell image

PHPxWorld - The resurgence of PHP meet-ups with Chris Morrell

Read article
Herd Executable Support and Pest 3 Mutation Testing in PhpStorm 2024.3 image

Herd Executable Support and Pest 3 Mutation Testing in PhpStorm 2024.3

Read article