2

I am using Refile gem to upload images on S3 in Rails 4 project. My requirement is to upload images for separate functionalities into two separate buckets on S3.

However, I could find documentation for setting up only one bucket. Is there anyway I can configure multiple S3 buckets with Refile?

Simmi Badhan
  • 493
  • 1
  • 5
  • 16

2 Answers2

2

Based on Simone's information, I implemented the code in my own project so it effectively works.

Refile.store and Refile.cache are included in Refile's constructor and is a shortcut from Refile.backends['store'] and Refile.backends['cache']. So you just need to add the backends to the @backends hash.

aws_base = {
  access_key_id: ENV['S3_ACCESS_KEY'],
  secret_access_key: ENV['S3_SECRET_KEY'],
  region: ENV['S3_REGION'],
}

aws_1 = aws_base.merge({bucket: "bucket-1"})
aws_2 = aws_base.merge({bucket: "bucket-2"})
cache = aws_base.merge({bucket: "caches"})

Refile.backends["backend_1"] = Refile::S3.new(prefix:"store", **aws_1)
Refile.backends["backend_2"] = Refile::S3.new(prefix:"store", **aws_2)
Refile.backends["shared_cache"] = Refile::S3.new(prefix:"store", **cache)

So that's how you set different backends, and to use them separately you just need to address them by name in the attachments initializers.

class FirstObject < ActiveRecord::Base
  attachment :images, store: 'backend_1', cache: 'shared_cache'
end

class SecondObject < ActiveRecord::Base
  attachment :images, store: 'backend_2', cache: 'shared_cache'
end
Lomefin
  • 1,173
  • 12
  • 43
1

Yes, it should be possible. Refile has a registry where it stores the various backends and by defaults it uses a backend called store which is initialized on boot.

Since you can configure the backend name per model, simply register new backends (such as store_foo and store_bar) pointing to different buckets and pass the names when you configure the corresponding models.

Simone Carletti
  • 173,507
  • 49
  • 363
  • 364
  • Thanks. But when I tried to define store1 and store2 variables in initializer, it gave error - undefined method `store1=' for Refile:Module. Also, how can I map different models with different stores? – Simmi Badhan Sep 08 '15 at 05:34