Report this

What is the reason for this report?

Spaces CDN with Rails Active Storage

Posted on August 26, 2021

I’m using Active Storage in a Rails 6 application with Spaces. I use “local” file storage for development and Spaces for production. I’d like to use the Spaces CDN feature, which I believe is possible with Active Storage “proxying” the files, but am unsure how to implement it. I’m unable to find a solid tutorial. I’ve followed tutorials suggesting “direct” routes for the CDN, but that doesn’t seem to work if you have different storage locations for different environments. Can anyone point to some setup instructions?



This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

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.

Heya,

Just came across this answer and decided to write some general guidelines for anyone who comes across this in the future despite the old question.

You’re right in that Active Storage can be used with Spaces. The CDN feature can also be enabled, but the implementation can depend on your specific use case and environment setup.

For handling different storage locations for different environments, you might have to tailor your configuration accordingly. That could involve setting different service configuration in each environment’s specific storage.yml file. So your production file would point to the Spaces service, while the development file would point to local storage.

As for the CDN setup, you will need to make sure that the CDN is enabled on your Spaces bucket and then use the CDN endpoint as the base URL for your ActiveStorage service.

There seems to be some ambiguity in the existing tutorials, unfortunately. You may find DigitalOcean’s documentation on this topic helpful, where the use of Spaces with Ruby on Rails is explained: Using Spaces with Ruby on Rails

Please remember that these instructions are general guidance and may need to be adapted to your specific setting.

Hope that this helps!

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.