Using DigitalOcean Spaces for Laravel Object Storage

Simon Bennett
Simon Bennett ยท Jun 07, 2020
Using DigitalOcean Spaces for Laravel Object Storage Artwork

Backup your first Laravel website for free SnapShooter Laravel backup Solutions

DigitalOcean offers a fantaic alternative to using S3 for object storage, it comes in alot cheaper and if your running your applications in DigitalOcean you can save on the latancy that providers interconnect can bring.

For a starting price of $5 per month, you get 250GB of storage and 1TB of bandwidth included. Extra storage costs is $0.02 per GB, and additional bandwidth is just $0.01 per GB.

The best part of of all of this is DigitalOcean Spaces is 100% compatible with AWS S3 meaning getting connected is very simple.

Configuring Laravel

As stated before the spaces has a S3 compatible API for the flysystem driver that comes with laravel is already suited for getting connected.

Make sure you have already pulled in the FlySystem S3 drivers into your code base if not:

$ composer require league/flysystem-aws-s3-v3

Once completed you can now add a new driver to config/filesystems.php I am going to call mine digitalocean. Note the extra option for endpoint, this overrides the default in the library which is set for AWS. You will find with other providers who do S3 compatable storage, this is the difference.

'digitalocean' => [ 'driver' => 's3', 'key' => env('DIGITALOCEAN_SPACES_KEY'), 'secret' => env('DIGITALOCEAN_SPACES_SECRET'), 'endpoint' => env('DIGITALOCEAN_SPACES_ENDPOINT'), 'region' => env('DIGITALOCEAN_SPACES_REGION'), 'bucket' => env('DIGITALOCEAN_SPACES_BUCKET'), ],

You will then need to edit your .env to contain these new enviroment varibles.


Note the region needs adding to the ENDPOINT and the REGION config. e.g. nyc1 You can learn about how to gather these keys How to Manage Administrative Access to Spaces

Using Spaces

Your now free to use your new DigitalOcean spaces driver in your laravel app.

You could call it directly

Storage::disk('digitalocean')->putFile('uploads', request()->file, 'public');

Or you may wish to edit the default driver in config/filesystems.php

I hope this is helpful, if you do have data on your server that you can't use object storage for and you wish to back it up why not checkout our file backup storage solutions