DEV Community

Cover image for Configure Scaleway S3 for Active Storage in Rails 6.0
François
François

Posted on • Edited on

Configure Scaleway S3 for Active Storage in Rails 6.0

Scaleway is a French web hosting company, founded by Xavier Niel in 1999. It provides physical and online dedicated servers, domain registration services and datacenters.

In 2020, Scaleway was the second player in France behind OVHCloud and the third in Europe.

If you want an alternative to Amazon AWS S3 storage services that we use @Kinoba, follow along this tiny and easy tutorial 😊


Scaleway part

Let's begin with the Scaleway config 👨🏻‍🔧

  • 1️⃣ First step: Create an account on Scaleway Elements:

👉🏻 https://www.scaleway.com/fr/object-storage/

  • 2️⃣ Second step: Create your S3 bucket by going to your Scaleway console and "Object Storage" tab.

Here, a help procedure if needed:
👉🏻 https://www.scaleway.com/en/docs/object-storage-feature/#-Operation-Procedures

  • 3️⃣ Third step: Generate your Access key and your Secret key 🔐 (do not forget to store them somewhere like 1Password or Bitwarden for example):

👉🏻 https://www.scaleway.com/en/docs/generate-api-keys/


Ruby On Rails part

The funniest part: integrating Scaleway into your Rails app 👩🏽‍💻

  • After installing ActiveStorage and creating the active_storage_tables migration, you can add the "aws-sdk-s3" gem to your Gemfile
# Gemfile
gem 'aws-sdk-s3', require: false
Enter fullscreen mode Exit fullscreen mode
  • Then, you can add your credentials to your .env file (do not forget to add your file to your .gitignore)
# .env
SCALEWAY_ACCESS_KEY_ID=YOUR_ACCESS_KEY_ID
SCALEWAY_SECRET_ACCESS_KEY_ID=YOUR_SECRET_ACCESS_KEY_ID
SCALEWAY_BUCKET_NAME=YOUR_BUCKET_NAME
Enter fullscreen mode Exit fullscreen mode
  • Add this config to you storage.yml with your personal keys and bucket
# storage.yml
test:
  service: Disk
  root: <%= Rails.root.join("tmp/storage") %>

local:
  service: Disk
  root: <%= Rails.root.join("storage") %>

scaleway:
  service: S3
  endpoint: https://s3.fr-par.scw.cloud # Scaleway API endpoint, depending on your region
  access_key_id: <%= ENV['SCALEWAY_ACCESS_KEY_ID'] %> # Do not forget to hide your secrets
  secret_access_key: <%= ENV['SCALEWAY_SECRET_ACCESS_KEY_ID'] %> # Do not forget to hide your secrets
  region: fr-par # Your bucket region, here it's Paris
  bucket: <%= ENV['SCALEWAY_BUCKET_NAME'] %> # Your bucket name
Enter fullscreen mode Exit fullscreen mode
  • Do not forget to tell Rails to use Scaleway storage in production environment
# production.rb
config.active_storage.service = :scaleway
Enter fullscreen mode Exit fullscreen mode
  • Finally, you can attach your files to your records as usual 🙌🏻
# user.rb
has_one_attached :avatar
Enter fullscreen mode Exit fullscreen mode
# users_controller.rb
@user.avatar.attach(params[:avatar])
Enter fullscreen mode Exit fullscreen mode

That's it 🎉

easy peasy gif


Check it out on Medium

François Loupias - Fullstack web developer @Kinoba

Top comments (0)