23

Does anyone know if there is a way to configure custom buckets for specific attachments?

class MyModel < ApplicationRecord
  ...

  has_one_attached :logo, bucket: 'custom_bucket'

  ...    
end

3 Answers 3

18

Although there isn't a way to use specific "buckets", one can pretty easily add multiple active storage configurations for multiple buckets (I believe introduced in v6.1):

https://edgeguides.rubyonrails.org/active_storage_overview.html#attaching-files-to-records

For example, you might have a "amazon_s3_cold" and an "amazon_s3_hot", they will have all the same configurations aside from the bucket. You may then configure your buckets accordingly on AWS.

# config/storage.yml

amazon_s3_hot:
   service: S3
   access_key_id: <%= Rails.application.credentials.dig(:aws, :access_key_id) %>
   secret_access_key: <%= Rails.application.credentials.dig(:aws, :secret_access_key) %>
   region: us-east-1
   bucket: my_hot_bucket
amazon_s3_cold:
   service: S3
   access_key_id: <%= Rails.application.credentials.dig(:aws, :access_key_id) %>
   secret_access_key: <%= Rails.application.credentials.dig(:aws, :secret_access_key) %>
   region: us-east-1
   bucket: my_cold_bucket

# controllers

class User < ApplicationRecord
    has_one_attached :avatar, service: :amazon_s3_hot
end

class DocumentRecord < ApplicationRecord
    has_one_attached :document_upload, service: :amazon_s3_cold
end

Note - hot/cold doesn't apply to the question directly, but provides some context. Hot/cold storage is a concept pertaining to cloud storage services that trades off costs for access frequencies.

2

You could follow a similar pattern to how a traditional database.yml file inherits settings which is just YML variables. My storage.yml file looks somewhat like this which allows me to store each Active Storage attachment type in their own folder.

The S3 provider which is what powers the DO provider requires a bucket name which I've just specified as 'default' but you could call it 'all' or 'general' and then override only the ones you care about.

(storage.yml)

do: &do
  service: S3
  endpoint: <%= Rails.application.credentials.dig(:digitalocean, :endpoint) %>
  access_key_id: <%= Rails.application.credentials.dig(:digitalocean, :access_key_id) %>
  secret_access_key: <%= Rails.application.credentials.dig(:digitalocean, :secret_access_key) %>
  region: 'nyc3'
  bucket: default

do_user_uploads:
  <<: *do
  bucket: user_uploads

(user.rb)

has_one_attached :upload, service: :do_user_uploads

Hope that helps, I came here looking for the same answer!

-3

There isn’t, sorry. Active Storage is designed for use with a single bucket.

2
  • 2
    How difficult would it be to add options to ActiveStorage to allow for different buckets for different security contexts. While there, it would also be good to allow developers to specify custom prefixes. It's often desirable for other systems/apps to access such storage for the purposes of async processing or other management. Nov 28, 2018 at 13:17
  • Although this more or less answers the question, it's misleading. Please include potential alternative solutions.
    – karns
    Oct 27, 2020 at 17:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.