请原谅我糟糕的英语。
在我们的系统中,我们使用Rails中的ActiveStorage来管理图像。 这次,我们将Rails从6.0.3.6更新到6.1.3.2,遇到了以下问题。 我想知道问题的原因以及如何解决。
更新前后S3中存储的图片URL发生变化,更新前存储的图片无法显示。
rails active_storage:update
rails db:migrate
源代码
class Image < ApplicationRecord
include ActiveStorageSupport::SupportForBase64
after_commit :process_variants
has_one_base64_attached :file
def provider_file_url
key = file.variant(resize_to_limit: [500, 500]).key
"#{ENV[‘BASE_URL']}/#{key}"
end
private
def process_variants
return unless file.attachment
file.variant(resize_to_limit: [500, 500]).processed
rescue StandardError
nil
end
end
# Call URL
image_instance.provider_file_url
Rails 更新前后生成相同的 URL。
Rails 更新前后生成的 URL 不同。
示例:
更新前
https://sample.net/variants/zvd2z2dafhrht3yy99l8w9zw54h8/3a8af9a41326f81dcc439e015ab90b5d9d078e2b00dd7d021c4d4ee650e5c6e0
更新后
https://sample.net/variants/zvd2z2dafhrht3yy99l8w9zw54h8/ac54d233b136b9f97f4d984f115d2e1c4695c2a0a3994c28d347637301445431
我比较了ActiveStorage 6.0.3.6和6.1.3.2的行为,发现了以下差异。 但是,我一直无法找到原因。
在两个地方添加了调试代码↓。
①
ActiveStorage::Variant
类的初始化(源代码)
class ActiveStorage::Variant
# omitted
def initialize(blob, variation_or_variation_key)
p '-----------------------------------------------------------------------------------------'
p 'blob'
p blob
p 'variation_or_variation_key'
p variation_or_variation_key
p '-----------------------------------------------------------------------------------------'
@blob, @variation = blob, ActiveStorage::Variation.wrap(variation_or_variation_key)
end
# omitted
end
②
encode
ActiveStorage::Variation
类的方法(源代码)
class ActiveStorage::Variation
attr_reader :transformations
class << self
# omitted
def encode(transformations)
p '-----------------------------------------------------------------------------------------'
p transformations
p '-----------------------------------------------------------------------------------------'
ActiveStorage.verifier.generate(transformations, purpose: :variation)
end
end
# omitted
end
运行provider_file_url方法
①
活动存储
6.0.3.6
"-----------------------------------------------------------------------------------------"
"blob"
#<ActiveStorage::Blob id: 999, key: "zvd2z2dafhrht3yy99l8w9zw54h7", filename: "sample.jpg", content_type: "image/jpeg", metadata: {"identified"=>true, "analyzed"=>true}, byte_size: 15789, checksum: "NwtXS6KRF1ty1WxXMicUew==", created_at: "2021-06-02 08:32:15">
"variation_or_variation_key"
{:resize_to_limit=>[500, 500]}
"-----------------------------------------------------------------------------------------"
活动存储
6.1.3.2
"-----------------------------------------------------------------------------------------"
"blob"
#<ActiveStorage::Blob id: 999, key: "zvd2z2dafhrht3yy99l8w9zw54h7", filename: "sample.jpg", content_type: "image/jpeg", metadata: {"identified"=>true, "analyzed"=>true}, byte_size: 15789, checksum: "NwtXS6KRF1ty1WxXMicUew==", created_at: "2021-06-02 17:32:15.000000000 +0900", service_name: "amazon">
"variation_or_variation_key"
#<ActiveStorage::Variation:0x00007fa2db230430 @transformations={:format=>"jpg", :resize_to_limit=>[500, 500]}>
"-----------------------------------------------------------------------------------------"
②
活动存储
6.0.3.6
"-----------------------------------------------------------------------------------------"
{:resize_to_limit=>[500, 500]}
"-----------------------------------------------------------------------------------------"
活动存储
6.1.3.2
"-----------------------------------------------------------------------------------------"
{:format=>"jpg", :resize_to_limit=>[500, 500]}
"-----------------------------------------------------------------------------------------"
作为测试,我添加了一个过程来删除编码方法中的 :format 键,现在 URL 的生成方式与更新之前一样。
导轨:6.0.3.6
红宝石:2.6.6
Gemfile.lock
activestorage (6.0.3.6)
actionpack (= 6.0.3.6)
activejob (= 6.0.3.6)
activerecord (= 6.0.3.6)
marcel (~> 1.0.0)
Rails 版本:6.1.3.2
Ruby 版本:2.6.6
Gemfile.lock
activestorage (6.1.3.2)
actionpack (= 6.1.3.2)
activejob (= 6.1.3.2)
activerecord (= 6.1.3.2)
activesupport (= 6.1.3.2)
marcel (~> 1.0.0)
mini_mime (~> 1.0.2)
这个问题有什么解决办法吗?从 6.0 升级到 6.1 后,S3 中对图像的引用发生了变化,并且图像未显示。