我正在使用rails 5.2,Shrine 2.19和tus server 2.3来上传文件
routes.rb
mount Tus::Server => '/files'
model,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'],
access_key_id: ENV['S3_ACCESS_KEY'],
secret_access_key: ENV['S3_SECRET_ACCESS_KEY'],
region: ENV['S3_REGION'],
retry_limit: 3
)
Tus::Server.opts[:redirect_download] = true
我的问题是我不能覆盖Shrine
类的generate_location
方法来将文件存储在AWS s3中的不同文件夹结构中。
所有文件都在s3://bucket/file_library/
(tus.rb中提供的前缀)中创建。我想要类似s3://bucket/file_library/:user_id/:parent_id/
文件夹结构的东西。
我发现Tus配置覆盖了我所有的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
['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
那么,我如何在S3中使用tus选项创建自定义文件夹结构(因为shrine选项不起作用)?
1条答案
按热度按时间pgpifvop1#
tus服务器上传根本不会触及Shrine,因此不会调用
#generate_location
,而是由tus-ruby-server
决定位置。请注意,tus服务器应该只作为一个临时存储,你仍然应该使用Shrine复制文件到一个永久存储(又名“提升”),就像定期直接上传。在升级时,
#generate_location
方法将被调用,因此文件将被复制到所需的位置;这一切都是在默认的神殿设置下自动发生的。