在我们的Ruby中,我希望每个帐户的附件都有一个单独的S3存储桶。我还希望桶名称可以从帐户的属性派生:
Account(id: 1, username: "johnny") # uses the "1-johnny" bucket
Account(id: 2, username: "peter") # uses the "2-peter" bucket
# ...
在Shrine中是否可以这样做?
答案 0 :(得分:1)
是。首先,使用default_storage
插件动态分配存储名称:
Shrine.plugin :default_storage, store: ->(record, name) do
"store_#{record.id}_#{record.username}"
end
# store_1_johnny
# store_2_peter
接下来,您使用dynamic_storage
插件根据标识符动态实例化S3存储:
Shrine.plugin :dynamic_storage
Shrine.storage /store_(\d+)_(\w+)/ do |match|
bucket_name = "#{match[1]}_#{match[2]}"
Shrine::Storage::S3.new(bucket: bucket_name, **s3_options)
end
# 1-johnny
# 2-peter