title: How to configure Rails to use the correct ActiveStorage storage service
body: <p>I'm running Rails 6.3 on Ruby 2.7.2p137</p>
  <p>My goal is to have a public S3 bucket and a private S3 bucket. I would like the public bucket so Rails will serve the S3 URL directly and avoid a redirect. And, once this is working, so I can put them in a CDN. And the private bucket for more secure files (realizing limited security through obscurity if the redirect URLs are served).</p>
  <p>In my <code>config/storage.yaml</code> I have the following:</p>
  <pre><code>test:
    service: Disk
    root: &lt;%= Rails.root.join(&quot;tmp/storage&quot;) %&gt;
  
  local:
    service: Disk
    root: &lt;%= Rails.root.join(&quot;storage&quot;) %&gt;
  
  # Use rails credentials:edit to set the AWS secrets (as aws:access_key_id|secret_access_key)
  amazon:
      service: S3
      access_key_id: &lt;%= Rails.application.credentials.dig(:aws, :access_key_id) %&gt;
      secret_access_key: &lt;%= Rails.application.credentials.dig(:aws, :secret_access_key) %&gt;
      region: us-east-1
      bucket: &lt;public-bucket-name&gt;
      public: true
  
  private:
      service: S3
      access_key_id: &lt;%= Rails.application.credentials.dig(:aws, :access_key_id) %&gt;
      secret_access_key: &lt;%= Rails.application.credentials.dig(:aws, :secret_access_key) %&gt;
      region: us-east-1
      bucket: &lt;private-bucket-name (absolutely different from public name)&gt;
      
  
  </code></pre>
  <p>in <code>config/environments/development.rb</code>:</p>
  <pre><code>config.active_storage.service = :local
  </code></pre>
  <p>in <code>config/environments/test.rb</code>:</p>
  <pre><code>config.active_storage.service = :test
  </code></pre>
  <p>in <code>config/environments/production.rb</code>:</p>
  <pre><code>config.active_storage.service = :amazon
  </code></pre>
  <p>In the models which will have attachments in the public service, I have the following (e.g.):</p>
  <pre><code>class User &lt; ApplicationRecord
      ...
      has_one_attached :avatar
  </code></pre>
  <p>In production, I expect it to use the default storage service (:amazon) set in the configuration file.</p>
  <p>For the attachments I want to store in the private bucket, I have (e.g.):</p>
  <pre><code>class Product &lt; ApplicationRecord
      ...
      has_one_attached :spec, service: :private
  </code></pre>
  <p>However, when I upload the spec file (using direct upload) in <code>ProductsController#create</code> or <code>ProductsController#update</code>, the <code>active_storage_blobs</code> has a new row, but the <code>service_name</code> is <code>amazon</code> and I can confirm the file has been uploaded to the public bucket rather than the private bucket.</p>
  <p>So, it appears that <code>service: :private</code> is completely ignored.</p>
  <p>How can I configure this so files attached to the Product are uploaded to the :private service?</p>