我正在使用Rails 5.2,Shrine 2.19和tus server 2.3进行可恢复文件上传
routes.rb
mount Tus::Server => '/files'
模型,file_resource.rb
class FileResource < ApplicationRecord
# adds an `file` virtual attribute
include ResumableFileUploader::Attachment.new(:file)
controllers / files_controller.rb
def create
file = FileResource.new(permitted_params)
...
file.save
config / initializers / shrine.rb
s3_options = {
bucket: ENV['S3_MEDIA_BUCKET_NAME'],access_key_id: ENV['S3_accESS_KEY'],secret_access_key: ENV['S3_SECRET_accESS_KEY'],region: ENV['S3_REGION']
}
Shrine.storages = {
cache: Shrine::Storage::S3.new(prefix: 'file_library/shrine_cache',**s3_options),store: Shrine::Storage::S3.new(**s3_options),# public: true,tus: Shrine::Storage::Tus.new
}
Shrine.plugin :activerecord
Shrine.plugin :cached_attachment_data
config / initializers / tus.rb
Tus::Server.opts[:storage] = Tus::Storage::S3.new(
prefix: 'file_library',bucket: ENV['S3_MEDIA_BUCKET_NAME'],region: ENV['S3_REGION'],retry_limit: 3
)
Tus::Server.opts[:redirect_download] = true
我的问题是我无法覆盖generate_location
类的Shrine
方法来将文件存储在AWS s3中的不同文件夹结构中。
所有文件都在s3://bucket/file_library/
(tus.rb中提供的前缀)中创建。我想要类似s3://bucket/file_library/:user_id/:parent_id/
的文件夹结构。
我发现Tus配置覆盖了我所有的resumable_file_uploader
类自定义选项,并且对上传没有影响。
resumable_file_uploader.rb
class ResumableFileUploader < Shrine
plugin :validation_helpers # NOT WORKS
plugin :pretty_location # NOT WORKS
def generate_location(io,context = {}) # NOT WORKS
f = context[:record]
name = super # the default unique identifier
puts "<<<<<<<<<<<<<<<<<<<<<<<<<<<<"*10
['users',f.author_id,f.parent_id,name].compact.join('/')
end
Attacher.validate do # NOT WORKS
validate_max_size 15 * 1024 * 1024,message: 'is too large (max is 15 MB)'
end
end
那么我该如何使用tus选项在S3中创建自定义文件夹结构(因为神社选项不起作用)?