【发布时间】: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