Configure Scaleway S3 for Active Storage in Rails 6.0
Tags: rails, activestorage, scaleway, s3
Published: 2021-03-26 08:20:08

*[Scaleway](https://www.scaleway.com/en/) is a French web hosting company, founded by [Xavier Niel](https://fr.wikipedia.org/wiki/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](https://kinoba.fr), 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/](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](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/](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

```ruby
# Gemfile
gem 'aws-sdk-s3', require: false
```  

- Then, you can add your credentials to your **.env** file (do not forget to add your file to your **.gitignore**)

```ruby
# .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
``` 

- Add this config to you **storage.yml** with your personal keys and bucket

```yaml
# 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
```  

- Do not forget to tell Rails to use Scaleway storage in **production** environment

```ruby
# production.rb
config.active_storage.service = :scaleway
```  

- Finally, you can attach your files to your records as usual 🙌🏻

```ruby
# user.rb
has_one_attached :avatar
```  
```ruby
# users_controller.rb
@user.avatar.attach(params[:avatar])
```  

---

## That's it 🎉 
![easy peasy gif](https://media.giphy.com/media/NaboQwhxK3gMU/giphy.gif)

---
Check it out on [Medium](https://kinoba.medium.com/configure-scaleway-s3-for-active-storage-in-rails-6-0-7502755842ad)  

François Loupias - Fullstack web developer [@Kinoba](https://kinoba.fr)