Rails 5.2, ActiveStorage and Digital Ocean

Rails Oct 08, 2018

Last time I wrote about ActiveStorage and Amazon S3, this time we do same with Digital Ocean. A great thing about Digital Ocean is that their pricing is affordable compared to many alternatives and they even throw in CDN without extra charge.

If you haven't already gone thru original post I suggest you do it now. Just jump to Rails 5.2, ActiveStorage and Amazon S3 article. If you don't want to go first round with Amazon AWS you can jump between these articles. I will mention differences between these articles on the way.

Setting up your storage

If you don't already have an account at Digital Ocean, now it's a great time to get one. If you sign up via my article you will receive $25 worth of free credits. Just click on any Digital Ocean link (like this one) to get claim your free credits.

Once you are in, head to spaces and click create space.

Currently Digital Ocean offers four different regions for your space. Choose one which suits you best. I'll go with Amsterdam in this article.

Next, you can opt in or out on CDN. I recommend using CDN on your production environment since it makes your file sharing more reliable as well as many times faster compared to service without CDN.

When you are creating a file store for your software I recommend that you don't enable file listing.

Choose a name for your store and create it.

To connect your software with Digital Ocean you need API keys. Open API from the left menu and scroll down a bit if you don't directly see Spaces Access Keys. Create a new key and in return, you will get Access key and Secret key.

That's it, your storage space is done.

Required changes to your Rails application

There isn't much to fix on your software since Digital Ocean Spaces is fully compatible with Amazon S3. Yes, no need to do any special tricks!

If you don't yet have a working example from the previous article you can do it now. The only thing you need to change is credentials on config/storage.yml and add after bucket endpoint:. It's that simple!

Just fill change your credentials, access_key_id and secret_access_key to config/credentials.yml.enc. Then fill in region, bucket and endpoint with information what is given on Digital Ocean spaces. All information is given on your space URL (except keys, they are on API page). For example:


would be

region: ams3
bucket: do-storage-example
endpoint: 'https://ams3.digitaloceanspaces.com'

Remember: Rails 5.2 uses encrypted credentials, change your keys with special command rails credentials:edit. If you get stuck on this, see my previous article.

And that's all! Now you are surfing on Digital Ocean Spaces!

Mika Peltomaa

Full Stack Hero who is not afraid to step out from comfort zone. Developing apps for web and mobile is close to heart, including Game Development as a Weekend Warrior.

Great! You've successfully subscribed.
Great! Next, complete checkout for full access.
Welcome back! You've successfully signed in.
Success! Your account is fully activated, you now have access to all content.