Cellar a S3-like service for Rails’ Active Storage

Introduced with the release of Rails 5.2 , Active Storage replaces previous solutions for storing user videos, images, PDF, etc..

In the documentation only three implementations are demonstrated and they all rely on cloud services. Today I am going to show you how to use it to be Clever Cloud compatible.

To follow this tutorial you must already have a Rails >= 5.2 application running on Clever Cloud with a PostgreSQL database. You can read this if you need a basic setup.

Tell Rails you want to use Active Storage

In your console, at the root of your rails project repository, type in $ rails active_storage:install. This will create two migration files under ./db/migrate. These migrations will create two new tables: active_storage_blobs and active_storage_attachments, which is everything we need to start storing user attachments without adding new columns to our already existent models. Let's run them with $ rake db:migrate to have a local database up to date. You can now use has_one_attached :<the name you want to give> on any of your models to add an attachment on it.

Setting up the application for Clever Cloud

As we do provide AWS S3 like storage at Clever Cloud, we will use AWS gem and define custom endpoints. Add gem "aws-sdk-s3", require: false to your Gemfile. Run $ bundle install. Then in config/environments/production.yml add config.active_storage.service = :clevercloud. Now in config/storage.yml add the following lines:

clevercloud:
  service: S3
  access_key_id: <%= ENV['CELLAR_ADDON_KEY_ID'] %>
  secret_access_key: <%= ENV['CELLAR_ADDON_KEY_SECRET'] %>
  region: us-east-1
  bucket: <%= ENV['CELLAR_ADDON_BUCKET_NAME'] %>
  endpoint: <%= ENV['CELLAR_ADDON_ENDPOINT'] %>

I must make clear here that we do not use regions at Clever Cloud, but the aws-sdk-s3 gem needs it; otherwise it throws an AWS::Errors::MissingRegionError error. And we do not want a 500 in production. It is the usage of endpoint that will give us the right endpoint generated by the gem.

Creating our storage on Clever Cloud

Under the organization where your Rails application is deployed, select Create, an add-on, Cellar S3 storage. When prompted Select the applications that will use this add-on: select your Rails application. Name it and let it be deployed. Once this is done, in your addon dashboard, create a bucket. Keep its name. Now, under Environment variables in your Rails application, add the following:

CELLAR_ADDON_BUCKET_NAME=<your bucket name>
CELLAR_ADDON_ENDPOINT=https://cellar-c2.services.clever-cloud.com

You can now commit the local files we have changed and push them to your Clever Cloud remote to enjoy Active Storage in production.

Do not forget to edit your safe params in your controllers, forms and permissions.

Happy storing!

Blog

À lire également

Create your own MCP client/server: as easy as 1-2-3 with Otoroshi

While Otoroshi with LLM already allows you to simplify the management of your various AI providers, access to models and integration with your teams, we have added simplified management of MCP clients and servers.
Company

Clever Cloud obtains HDS (Health Data Hosting) certification

Clever Cloud achieves HDS Certification, enabling it to host health data in France. Clever Cloud, Europe's leading provider of Platform as a Service cloud solutions, today announced that it has been awarded the Hébergeur de Données de Santé (HDS) certification, in its updated version effective May 16, 2024, for all 6 activities in the standard. This certification reinforces Clever Cloud's position as a trusted partner for companies and organizations in the healthcare sector.
Press

Clever Tools: a year of enhancements for your deployments, on the road to v4

A command line interface (CLI) is at the core of developer experience. At Clever Cloud, we have been providing Clever Tools for almost 10 years.
Engineering Features