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

SuperBOL: The COBOL revolution in the Cloud

COBOL, a programming language that is over 60 years old, continues to power a large proportion of the IT systems of the world's major companies, particularly in the financial and insurance sectors.
Features

Clever Cloud welcomes the first startups to the UP Programme

Clever Cloud is proud to announce the arrival of the first five startups selected to join its UP Programme, an initiative dedicated to supporting young technology companies in their growth phase.
Company

A minor update resulted in a cascade of errors: how it went wrong, what we’ve learnt

On Friday, August 2nd, 2024 Clever Cloud’s platform became very unstable, leading to downtime of varying duration and scope, for customers using services on the EU-FR-1 (PAR) region, and remote zones depending on the EU-FR-1 control plane (OVHcloud, Scaleway, and Oracle). Privates and on-premise zones weren’t impacted.
Company Engineering