Rails 5.2 Shrine and Tus服务器:无法创建用于保存文件的自定义文件夹结构

问题描述 投票:0回答:1

我正在使用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

[我的问题是我无法覆盖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中创建自定义文件夹结构(因为神社选项不起作用?

ruby amazon-s3 ruby-on-rails-5 shrine tus
1个回答
0
投票
一个tus服务器上载根本不涉及Shrine,因此不会调用#generate_location,而是由tus-ruby-server决定位置。
© www.soinside.com 2019 - 2024. All rights reserved.