Adapting An Existing AWS S3 Application To DigitalOcean Spaces

We set out to give WordPress users an easy solution to offload their Media Library to Spaces

Adapting An Existing AWS S3 Application To DigitalOcean Spaces

We built the Media Library Folders Pro to help WordPress users better manage their Media Library and files with real folders. Many Media Library Folders Pro users rely on the plugin to help them manage especially large Media Libraries - the type that can quickly fill up a server.

These are the types of users who can benefit most from offloading their files to a service like Spaces and serving them either directly, or through a CDN.

In the WordPress space, however, there's no core functionality to help users offload their Media Library to a service like Spaces. As DigitalOcean users ourselves, we understand the benefit of being able to work within the DigitalOcean platform, which is why we set out to give WordPress users an easy solution to offload their Media Library to DigitalOcean Spaces.

In this post, we'll detail how we adapted our existing Media Library Folders Pro S3 and Spaces extension to work with DigitalOcean Spaces.

Launched in November 2017, Spaces is DigitalOcean’s object storage service. It has a number of similarities to AWS S3, though it's notably easier for end users to configure.

In fact, the two are designed to be interoperable, according to the DigitalOcean Spaces API:

  • The Spaces API aims to be interoperable with Amazon's AWS S3 API. In most cases, when using a client library, setting the "endpoint" or "base" URL to ${REGION}.digitaloceanspaces.com and generating a Spaces key to replace your AWS IAM key will allow you to use Spaces in place of S3.
  • Because of this interoperability, it was easy for us to take our existing S3 application, Media Library Folders Pro S3 and Spaces, and adapt it to work with Spaces.

To make an existing AWS S3 app compatible with Spaces, you just need to make two tweaks:

  • Change the connect string
  • Add a list of Spaces' locations

Here's how we did it…

Step 1: Change The Connect String

By default, the AWS SDK will attempt to connect to the endpoint of ‘https://s3.amazonaws.com/’. In order to make your app work with Spaces, you need to explicitly change this to connect to Spaces:

        try {
          $this->s3 = new Aws\S3\S3Client([
              'version'     => 'latest',
              'region'      => $region,
              'endpoint'    => 'https://nyc3.digitaloceanspaces.com',
              'credentials' => [
                  'key'     => YOUR_SPACES_ACCESS_KEY_ID,
                  'secret'  => YOUR_SPACES_SECRET_ACCESS_KEY
              ]
          ]);
        }

Step 2: Add Spaces Regions

In order to let users choose their preferred location, you also need to add a list of Spaces locations like so:

  public function get_do_regions() {

        $regions = array(
            'tor1'  => 'Toronto, Ontario — Canada', 
            'sfo1'  => 'San Francisco, California 1',
            'sfo2'  => 'San Francisco, California 2',
            'nyc1'  => 'New York City, New York 1',
            'nyc2'  => 'New York City, New York 2',
            'nyc3'  => 'New York City, New York 3',
            'lon1'  => 'London — UK',
            'fra1'  => 'Frankfurt — Germany',
            'ams2'  => 'Amsterdam — Holland 2',
            'ams3'  => 'Amsterdam — Holland 3',
            'blr1'  => 'Bangalore — India',
            'sgp1'  => 'Singapore'
    );

    return $regions;
  }

That's All There Is To It - But Test!

After making the two tweaks above, your S3 application will work with DigitalOcean’s Spaces.

In order to test our modified app's functionality, we:

Created a new Space at Spaces
Generated a Spaces access key and secret
Added them to our app where the AWS S3 credentials would have previously gone

And just like that, we were able to successfully connect to Spaces and start uploading and manipulating files with no further changes to our AWS S3 app.

Spaces Is A Great Alternative To AWS S3

Spaces makes a great alternative to AWS S3 for several reasons:

  • The setup process is much simpler - there's no need to fiddle with bucket policies or IAM users.
  • It's part of the DigitalOcean ecosystem, which is a big benefit if you're already hosting with DigitalOcean.
  • Pricing starts at as little as $5 per month for 250 GB of storage and 1 TB of outbound transfer. And it's easy to scale up if you need more resources.
  • You can use it to host static web assets.

Because of those benefits, we wanted to give Media Library Folders Pro users the option to take advantage of Spaces. And thanks to Spaces interoperability, we were able to easily get up and running with this new functionality for our users.