ruby-on-railsamazon-s3rails-activestorageshrine

Using Multiple Rails ActiveStorage Services


I am using ActiveStorage for uploading PDFs and images. The PDFs need to be stored locally because of some privacy concerns, while the images need to be stored using Amazon S3. However, it looks like ActiveStorage only supports setting one service type per environment (unless you use the mirror functionality, which doesn't do what I need it to in this case).

Is there a way to use different service configs within the same environment? For example, if a model has_one_attached pdf it uses the local service:

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

And if another model has_one_attached image it uses the amazon service:

amazon:
  service: S3
  access_key_id: ""
  secret_access_key: ""

Solution

  • Rails 6.1 now supports this.

    As per this article, you can specify the service to use for each attached:

    class MyModel < ApplicationRecord
      has_one_attached :private_document, service: :disk
      has_one_attached :public_document,  service: :s3
    end