加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – Rails:将Tempfile传递给Sidekiq

发布时间:2020-12-17 03:18:38 所属栏目:百科 来源:网络整理
导读:我在一个邮件端点上工作,将附件收集为Tempfile,然后我需要将它们传递给Sidekiq Worker以将它们上传到AWS. 我的问题是,我一直在试图坚持Tempfile,然后在我的工作人员中打开它.我不知道我应该打开我的Tempfile(路径,文件名……). 这是我的函数,它将调用worker
我在一个邮件端点上工作,将附件收集为Tempfile,然后我需要将它们传递给Sidekiq Worker以将它们上传到AWS.

我的问题是,我一直在试图坚持Tempfile,然后在我的工作人员中打开它.我不知道我应该打开我的Tempfile(路径,文件名……).

这是我的函数,它将调用worker:

if @email
    # Remove Tempfile autodelete
    @email.attachments.each {|t| ObjectSpace.undefine_finalizer(t.tempfile)}

    # Griddler Email to hash for Sidekiq
    email = {
        attachments: @email.attachments.map {|att| {
            type: att.content_type,name: att.original_filename
        }},raw_text: @email.raw_text,raw_html: @email.raw_html,from: @email.from,subject: @email.subject,to: @email.to,cc: @email.cc
    }

    EmailResponseWorker.perform_async email
end

这里我使用ObjectSpace.undefine_finalizer(t.tempfile)来禁用自动删除.

然后在我的Sidekiq工人:

def perform(email)
  @email = email

  attachments = @email['attachments'].inject([]) do |arr,file|
    object = S3_BUCKET.objects["attachments/#{SecureRandom.uuid}/#{file['name']}"].write(Tempfile.open(file['name']),acl: :public_read)
    arr << {url: object.public_url.to_s,type: file['type'],name: file['name']}
  end
end

这里附件[‘name’]是文件的名称.

解决方法

从tempfile获取路径并将其作为通常的文件路径处理:

path: att.tempfile.path

它是tempfile本身的路径,original_filename是客户端传递的文件名,而不是您需要的文件名.

工作成功完成后,请不要忘记取消链接.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读