Using Rails ActiveStorage with DigitalOcean Spaces CDN Custom Subdomain?

Posted April 13, 2019 5.2k views
Ruby on RailsDigitalOceanCDNUbuntu 18.04

I’m working on setting up a custom subdomain for my site’s Spaces CDN and was wondering if anyone has any advice on how to get my Rails app to actually return the custom subdomain URL for images instead of the domain.

My images are being loaded from{a_bunch_of_other_metadata}, despite my CDN being set up with a subdomain of

I have this in my config/storage.yml (it’s called amazon but it’s really DO Spaces):

  service: S3
  access_key_id: <%= Rails.application.credentials.dig(:digitalocean, :access_key_id) %>
  secret_access_key: <%= Rails.application.credentials.dig(:digitalocean, :secret_access_key) %>
  region: sfo2
  bucket: vglist
  endpoint: ''

I tried changing the endpoint but that didn’t seem to work. The custom subdomain is set up and everything, I just need to configure Rails and ActiveStorage to actually use it.


These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Submit an Answer
4 answers

FYI, the active storage gods have now determined we are worthy of this trivial feature. It’s called “proxying,” and it’s been released in Rails 6.1.0 (December 09, 2020)

  • @Wilson29thID do you happen to know how to use this? I can’t really seem to figure it out since the docs on it are pretty sparse.

    I don’t think this really fixes the problem since you can’t specify a special URL (the CDN URL, in this case), unless you’re supposed to just replace the endpoint?

@connoshea Not sure if you’re still in need of a solution to this as the post is rather old, but as I came here via a search, I thought providing one info would be good for posterity.

I’m not aware of anything built in to ActiveStorage as it is quite light in its implementation, so you will have to extend the class for the storage engine that you are using and override some methods to achieve what you want.

This stack overflow post has some details:

Any update? I want to use Space CDN, but I cann’t find the way to change the url to CDN address.

Try adding this to your application.rb or environment file:

config.asset_host = <CDN>