【问题标题】:Rails 6 Active storage sidekiq file upload background image on S3Rails 6主动存储sidekiq文件在S3上上传背景图片
【发布时间】:2021-03-07 13:34:37
【问题描述】:

我正在开发 Rails 6 API 并使用 S3 上的活动存储上传多个文件。

我需要上传大文件和很多文件,所以我正在考虑使用后台作业。

根据我的研究,active_storage 默认使用背景,所以不需要使用 sidekiq,是这样吗?

如果不使用后台作业,那么如何在 UploadAttachmentWorker 中创建附件?

附件控制器

def create
    @attachment_serializer = Array.new
    params[:data][:attachments].each do |attachment_params|
      params[:attachment_data] = attachment_params[1]
      @attachment = @account.attachments.create(attachments_params)
      @attachment_serializer << @attachment
  end
    if @attachment.save
      render json: AttachmentSerializer.new(@attachment_serializer, meta: {
        message: "Attachment Created Successfully"
      }).serializable_hash, status: :created
    else
      render json: {errors: format_activerecord_errors(@attachment.errors)},
        status: :unprocessable_entity
    end
  end
  def attachments_params
    params.require(:attachment_data).permit :colour, :layout, :page_size, 
    :scale,:print_sides,:print_pages_from, :print_pages_to, :attachment
  end

UploadAttachmentWorker

class UploadAttachmentWorker
  include Sidekiq::Worker
    def perform()

    end
end

请帮帮我。

【问题讨论】:

  • 处理类似问题。这里列出了几种方法(我自己特别关注涉及线程的方法)stackoverflow.com/questions/2162046/…
  • 不能使用sidekiq,因为我们不能将文件对象传递给它。消耗时间的两个主要因素是文件的大小,甚至更大的对 S3 的请求数量。与 5-6 KB 文件相比,1 MB 文件将花费更少的时间,因为发送到云存储服务的请求数量较多(我已经通过邮递员对其进行了测试)。第一个解决方案是探索主动存储的直接上传选项。第二个可以是,允许用户在前端选择任意数量的文件。将文件一一发送到后端并显示进度(上传的 n 个文件中的 1 个)。希望这会有所帮助。

标签: ruby-on-rails api file-upload background sidekiq


【解决方案1】:

我不熟悉 S3,但我对 Cloundinary 做了类似的事情。我需要上传 PDF 文件,所以我自己创建了一个后台作业。话虽这么说,我可能错了,但我不认为 active_storage 默认使用背景。

您可以在那里查看我的工作:BackgroundJob triggered multiple times on save ( Rails 5, Sidekiq)

【讨论】:

    猜你喜欢
    • 2012-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-23
    • 2018-07-11
    • 2018-06-19
    • 2017-09-17
    • 1970-01-01
    相关资源
    最近更新 更多