Different storage per attachment

Due to legal / privacy constraints, I would like to be in control of the location where the files are stored. For example keep files in the US for US visitors, in France for french visitors…

I can create multiple buckets in those locations (with various providers, which Shrine abstracts!), but how can I dynamically select the location for files? I am using Shrine with ActiveRecord, every attachment will be on a record belonging to a user, so I can have something like storage = "avatars_${record.user.storage_location} returning the correct storage.

I am not sure on how to do this properly with Shrine (let’s assume Shrine 3), any hints?

Hi, you can use a combination of dynamic_storage and default_storage plugins, for example:

Shrine.plugin :default_storage
Shrine.plugin :dynamic_storage

Shrine.storage /avatars_(\w+)/ do |match|
  Shrine::Storage::S3.new(bucket: match[1], ...)

Shrine::Attacher.default_store do
1 Like

Thanks, I completely missed those plugins!

Always amazed at how flexible Shrine is :slight_smile:

1 Like