Cover image for How to Set Up and Use Object Storage in Laravel 6

How to Set Up and Use Object Storage in Laravel 6

erikaheidi profile image Erika Heidi ・3 min read

Laravel is an open-source PHP framework that provides a set of tools and resources to build modern PHP applications.

In this short guide, we'll set up an existing Laravel 6 application to use an S3-compatible object storage service for storing user generated content. This setup was created for a Laravel 6 application running on top of an Ubuntu 18.04 LEMP stack, using DigitalOcean Spaces as S3-compatible object storage service.

This short guide is part of the tutorial on How to Set Up a Scalable Laravel 6 Application using Managed Databases and Object Storage, which contains detailed instructions on how to build this setup using a demo application.

Laravel uses league/flysystem, a filesystem abstraction library that enables a Laravel application to use and combine multiple storage solutions, including local disk and cloud services. An additional package is required to use the s3 driver.

We'll use Composer to install this package. From the application's directory, run:

composer require league/flysystem-aws-s3-v3

After installing the required packages, we can update the application to connect to the object storage. First, we'll open the .env file to set up configuration details such as keys, bucket name, and region for your object storage service.

Open the .env file using your editor of choice.

Include the following environment variables, replacing the example values with your own object store configuration details:


Save and close the file when you're done. Now open the config/filesystems.php file.

Within this file, we'll create a new disk entry in the disks array. We'll name this disk spaces, and we'll use the environment variables we've set in the .env file to configure the new disk. Include the following entry in the disks array:

'spaces' => [
   'driver' => 's3',
   'key' => env('DO_SPACES_KEY'),
   'secret' => env('DO_SPACES_SECRET'),
   'endpoint' => env('DO_SPACES_ENDPOINT'),
   'region' => env('DO_SPACES_REGION'),
   'bucket' => env('DO_SPACES_BUCKET'),

Still in the same file, locate the cloud entry and change it to set the new spaces disk as default cloud filesystem disk:

'cloud' => env('FILESYSTEM_CLOUD', 'spaces'),

Save and close the file when you're done editing. From your controllers, you can now use the Storage::cloud() method as a shortcut to access the default cloud disk. This way, the application stays flexible to use multiple storage solutions, and you can switch between providers on a per-environment basis.

As an example, let's say you have a travel photo album application where you save photos from places you have visited. A form allows you to upload a photo and select a place or create a new one. This is how an upload controller would look like, using the new spaces S3-compatible disk:


namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Photo;
use App\Place;
use Illuminate\Support\Facades\Storage;

class PhotoController extends Controller
   public function uploadForm()
       $places = Place::all();

       return view('upload_photo', [
           'places' => $places

   public function uploadPhoto(Request $request)
       $photo = new Photo();
       $place = Place::find($request->input('place'));

       if (!$place) {
           //add new place
           $place = new Place();
           $place->name = $request->input('place_name');
           $place->lat = $request->input('place_lat');
           $place->lng = $request->input('place_lng');

       $place->visited = 1;

       $photo->image = $request->image->store('/', 'spaces'); #save image
       Storage::setVisibility($photo->image, 'public'); #make it public

       return redirect()->route('Main');

This file is also available on Github.

Once the photo is saved to the database, you can obtain its public URL with a call to the Storage::url method. The following code shows an example of how to obtain the image URL from a Blade template:

<img src="{{ Storage::disk('spaces')->url($photo->image) }}" />

For detailed instructions and examples, please check our full guide on How to Set Up a Scalable Laravel 6 Application using Managed Databases and Object Storage.

Posted on by:

erikaheidi profile

Erika Heidi


Code sorceress and machine enchantress. Passionate about writing and creating technical content for a variety of audiences.


DigitalOcean is the cloud of choice for developers around the world to create modern applications.


markdown guide