将 Rails 更新到 6.1.3.2 后,存储在 S3 中的图像的 URL 发生变化

问题描述 投票:0回答:1

请原谅我糟糕的英语。

在我们的系统中,我们使用Rails中的ActiveStorage来管理图像。 这次,我们将Rails从6.0.3.6更新到6.1.3.2,遇到了以下问题。 我想知道问题的原因以及如何解决。

出现的问题

更新前后S3中存储的图片URL发生变化,更新前存储的图片无法显示。

重现步骤

  • 将 Rails 从 6.0.3.6 更新到 6.1.3.2
  • 奔跑
    rails active_storage:update
  • 奔跑
    rails db:migrate
  • 打开包含 Rails 更新之前保存的图像的页面

源代码

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)
ruby-on-rails ruby amazon-s3 rails-activestorage
1个回答
0
投票

这个问题有什么解决办法吗?从 6.0 升级到 6.1 后,S3 中对图像的引用发生了变化,并且图像未显示。

© www.soinside.com 2019 - 2024. All rights reserved.