В последние дни я пытаюсь добавить новую конфигурацию хранилища для своего приложения, чтобы мы могли использовать Cloudinary для изображений и Digital Ocean Spaces для других файлов. И после изменений, которые, как я предполагал, были необходимы для этого, когда я пытаюсь получить доступ к атрибуту старой модели, связанному с Attacher ImageUploader, я получаю следующую ошибку:
Shrine::Error (storage :store isn't registered on ImageUploader)
The thing is: У меня действительно не определено хранилище "store", но это потому, что я пытаюсь иметь image_store
и raw_store
.
Вот мой config/initializers/shrine.rb
:
# config/initializers/shrine.rb
require "shrine"
require "shrine/storage/file_system"
require "shrine/storage/cloudinary"
require "shrine/storage/s3"
Shrine.plugin :activerecord
Shrine.plugin :cached_attachment_data # for retaining the cached file across form redisplays
Shrine.plugin :restore_cached_data # re-extract metadata when attaching a cached file
Shrine.plugin :rack_file # for non-Rails apps
Shrine.plugin :default_storage, store: :image_store
s3_options = {
access_key_id: ENV['DO_SPACE_ACCESS_KEY_ID'],
secret_access_key: ENV['DO_SPACE_SECRET_ACCESS_KEY'],
bucket: ENV['DO_SPACE_SECRET_BUCKET'],
endpoint: "https://#{ENV['REGION']}.digitaloceanspaces.com",
region: ENV['REGION']
}
if Rails.env.development? || Rails.env.test?
Shrine.storages = {
raw_cache: Shrine::Storage::FileSystem.new("public", prefix: "uploads/raw_cache"), # temporary
raw_store: Shrine::Storage::FileSystem.new("public", prefix: "uploads/raw_store"), # permanent
image_cache: Shrine::Storage::FileSystem.new("public", prefix: "uploads/image_cache"), # temporary
image_store: Shrine::Storage::FileSystem.new("public", prefix: "uploads/image_store") # permanent
}
else
Shrine.storages = {
raw_cache: Shrine::Storage::S3.new(
prefix: "raw_cache", upload_options: { acl: 'public-read' }, **s3_options
), # for direct uploads
raw_store: Shrine::Storage::S3.new(
prefix: "raw_store", upload_options: { acl: 'public-read' }, **s3_options
),
image_cache: Shrine::Storage::Cloudinary.new(prefix: "image_cache", resource_type: 'image'), # for direct uploads
image_store: Shrine::Storage::Cloudinary.new(prefix: "image_store", resource_type: 'image'),
}
end
А вот и мой ImageUploader:
class ImageUploader < Shrine
plugin :default_storage, cache: :image_cache, store: :image_store
plugin :validation_helpers
plugin :data_uri
plugin :infer_extension
plugin :upload_options, store: ->(io, context) do
[...]
end
plugin :url_options, store: -> (io, **options) do
[...]
end
# I even tried that one, but no success
Attacher.default_store :image_store
Attacher.validate do
validate_max_size 5*1024*1024, message: "is too large (max is 5 MB)"
end
end
Может ли кто-нибудь мне в этом помочь?