【问题标题】:Active Storage raises ActiveSupport::MessageVerifier::InvalidSignatureActive Storage 引发 ActiveSupport::MessageVerifier::InvalidSignature
【发布时间】:2018-06-18 03:53:30
【问题描述】:

为了使用 Active Storage 将图像文件导入 Rails 应用程序,我编写了这样的 Rake:

task :import_file => :environment do
  path = Rails.root.join("tmp", "sample.jpg")
  data = File.read(path)

  post = Post.first
  post.image.attach(data)
end

当我执行这个任务时,我得到了一个异常ActiveSupport::MessageVerifier::InvalidSignature

我怎样才能避免这个错误?

Post模型的源码为:

class Post < ApplicationRecord
  has_one_attached :image
end

我使用默认的config/storage.yml

test:
  service: Disk
  root: <%= Rails.root.join("tmp/storage") %>

local:
  service: Disk
  root: <%= Rails.root.join("storage") %>

Rails 的版本是 5.2.0.beta2。

【问题讨论】:

    标签: ruby-on-rails rails-activestorage


    【解决方案1】:

    Edge API document,我找到了答案。

    desc "Import file"
    task :import_file => :environment do
      path = Rails.root.join("tmp", "sample.jpg")
    
      post = Post.first
      File.open(path) do |io|
        post.image.attach(io: io, filename: "sample.jpg")
      end
    end
    

    【讨论】:

    • 小心:您正在为每个图像打开一个新的文件句柄。您必须关闭每个打开的文件,或者使用 File.open 的块形式,这将是执行此操作的首选方式。
    • @FrançoisBeausoleil 出于好奇,如果文件处理程序没有关闭会发生什么?
    • @stevec 如果你不关闭文件句柄,你可能会用完文件句柄。每个进程都有限制,根据您的操作系统,限制可能非常低,大约为 1000。
    【解决方案2】:

    把我的答案留在这里,以防万一有人遇到和我一样的问题。

    我犯了一个愚蠢的错误,即在创建variant 时没有在参数中传递resize 键:

    image_tag user.profile_photo.variant('200x200')
    

    应该已经过去了:

    image_tag user.profile_photo.variant(resize: '200x200')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-06-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-02
      • 1970-01-01
      相关资源
      最近更新 更多