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,
config/credentials.yml.enc. Then fill in
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:
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!