Configure Statamic 3 to use Digital Ocean's Spaces for your asset container

It's easier than ever to use object storage to make your Statamic site faster

Published June 12th 2020

Digital Ocean has established itself as a much more developer friendly service than Amazon's powerful, but complex web services catalogue. Spaces, its rival to Amazon's S3 object storage service is a great example of this. Not only is it much more simple to set up (compared to S3), it's also much easier to understand how much you'll be paying at the end of the month, with Digital Ocean charging a flat $5 fee per month, per space.

While Amazon has reduced the cost difference between the products significantly since Digital Ocean launched Spaces back in 2017 (at the time, it cost $95 for the same amount of storage and bandwidth, with the same amount of data costing just under $7 per month in 2020), the added complexity makes it a less attractive option for those who don't have a dedicated infrastructure team.

At my day job, we use load-balanced servers. While with Statamic, we could host the content on the server (as we use a separate CMS server to edit the content before pushing to the live servers), we'd prefer to avoid committing assets like images. Also, because we have visitors from all over the world, the speed of the site benefits massively from using a CDN, which Spaces has built in at no extra cost (something that costs extra with Amazon's suite of tools).

Changing Statamic 3's default container to use Digital Ocean spaces

First of all, I'm going to assume you already have a Digital Ocean account, that you've created a space, and have your credentials saved.

The first thing you're going to want to do is install the Flysystem S3 driver:

composer require league/flysystem-aws-s3-v3

Next, we're going to want to add a new disk in Laravel's config/filesystems file. We could use the default S3 disk configuration, but to avoid confusion in the future, we'll create a disk for spaces.

Open config/filesystems.php and the disks array. You'll a disk called S3. Simply duplicate the object and change the key to spaces. You will also want to change the environment keys to something else. Once you're done, you should have something like this:

'disks' => [

'local' => [
    'driver' => 'local',
    'root' => storage_path('app'),
],

'public' => [
    'driver' => 'local',
    'root' => storage_path('app/public'),
    'url' => env('APP_URL').'/storage',
    'visibility' => 'public',
],

's3' => [
    'driver' => 's3',
    'key' => env('AWS_ACCESS_KEY_ID'),
    'secret' => env('AWS_SECRET_ACCESS_KEY'),
    'region' => env('AWS_DEFAULT_REGION'),
    'bucket' => env('AWS_BUCKET'),
    'endpoint' => env('AWS_URL'),
],

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

'assets' => [
    'driver' => 'local',
    'root' => public_path('assets'),
    'url' => '/assets',
    'visibility' => 'public',
],

The next step is to add the new environmental variables to your .env file.

DO_SPACES_KEY=[YOURKEY]
DO_SPACES_SECRET=[YOURSECRET]
DO_SPACES_ENDPOINT=https://[YOURREGION].digitaloceanspaces.com
DO_SPACES_REGION=[YOURREGION]
DO_SPACES_BUCKET=[YOURBUCKET]

Let's save your Space is in Amsterdam (AMS3) and it's called statamic, then your config would look something like:

DO_SPACES_KEY=yourspaceskeyfromdigitalocean
DO_SPACES_SECRET=yoursecretkeyfromdigitalocean
DO_SPACES_ENDPOINT=https://ams3.digitaloceanspaces.com
DO_SPACES_REGION=ams3
DO_SPACES_BUCKET=statamic

If you've successfully connected your Spaces account to your Statamic account, the last thing to do is to go to content/assets/assets.yaml (or whatever your container is called). Open the file and change the value of disk from assets to spaces.

Assuming everything is done, then you should be able to upload a file via the Assets link in the sidebar and see the file in your Spaces file directory.