ActiveStorage with DigitalOcean Spaces

You are developing a Rails 5.2 application with ActiveStorage and don’t want to use AWS S3? You can also use DigitalOcean’s Spaces since it’s S3 compatible. Here’s a short example on how to store your assets on DigitalOcean Spaces:

1. Add the aws-sdk-s3 gem to your Gemfile:
gem 'aws-sdk-s3'
2. Don’t forget to bundle:
bundle
3. Add a DigitalOcean storage provider block in your config/storage.yml:
digitalocean:
  service: S3
  access_key_id: <%= ENV.fetch('ACCESS_KEY')% >
  secret_access_key: <%= ENV.fetch('SECRET_ACCESS_KEY') %>
  region: <%= ENV.fetch('REGION') %>
  bucket: <%= ENV.fetch('BUCKET_NAME') %>
  endpoint: 'https://ams3.digitaloceanspaces.com'

This leaves us with four env variables that must be set:

  • ACCESS_KEY: The access key can be obtained from the DigitalOcean settings.
  • SECRET_ACCESS_KEY: The be generated along the ACCESS_KEY.
  • REGION: The region that you’ve selected during the bucket creation (e.g. ams3).
  • BUCKET_NAME: The bucket name you’ve chosen.
4. Set the new storage provider for your environment:

In your config/environments/development.rb switch the :local provider with the new :digitalocean one.

config.active_storage.service = :digitalocean
5. Start uploading files to DigitalOcean Spaces!