'ActiveStorage - has_one_attached ignores service option

In my Ruby on Rails 6.1.3.2 application, I'm trying to has_one_attached service option to upload one of the model attachments to the separate S3 bucket. Here is how my storage.yml looks like:

test:
  service: Disk
  root: <%= Rails.root.join("tmp/storage") %>

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

amazon:
  service: S3
  access_key_id: <%= ENV['AWS_ACCESS_KEY_I'] %>
  secret_access_key: <%= ENV['AWS_SECRET_ACCESS_KEY'] %>
  region: us-east-2
  bucket: <%= ENV['AWS_BUCKET'] %>

amazon_logos_images:
  service: S3
  access_key_id: <%= ENV['AWS_ACCESS_KEY_I'] %>
  secret_access_key: <%= ENV['AWS_SECRET_ACCESS_KEY'] %>
  region: us-east-2
  bucket: <%= ENV['AWS_LOGOS_BUCKET'] %>

In my production.rb I have the following Active Storage configuration:

config.active_storage.service = :amazon

My Logo model looks like this:

class Logo < ApplicationRecord
  has_one_attached :image, service: :amazon_logos_images
end

Unfortunately, when I create a new Logo record, the image is uploaded to the amazon bucket instead of amazon_logos_images. Any idea why the service option is ignored by the has_one_attached method?



Solution 1:[1]

If you come from 6.0:

If our project is already using Active Storage and when we upgrade to Rails 6.1, we should run rake app:update to make sure service_name column is added to the internal ActiveStorageBlob model.

https://blog.saeloun.com/2020/02/03/rails-allows-configure-service-for-attachments-to-activestorage.html

Otherwise check that AWS_LOGOS_BUCKET is properly being populated (i.e. is not the wrong bucket)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 estani