Back to clever-cloud.com

Well, if you need some logos, we provide these:

svg ·  png
Red Clever Cloud logo
svg ·  png
svg ·  png
White Clever Cloud logo
svg · png
White Clever Cloud logo
svg · png
Posted at May 28, 2020 — Engineering

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!

Tags:

Check out our Blog

We write about programming, security and Clever Cloud products

Join thousands of developers already using Clever Cloud. And try it for free!

Create your account now to start your apps and collaborate within a few minutes. For free with 20€ offered.

By signing up, your agree to our terms of use
Or
Sign in with Github