通过 Carrierwave 使用 s3 上的现有图像?

发布于 2025-01-09 11:58:29 字数 1547 浏览 1 评论 0原文

我正在尝试使用 s3 上的现有图像并将它们与载波对象相关联。这些图像存储在与我的上传器的 store_dir 不同的位置。

我目前使用remote_image_url进行迁移设置,但速度非常慢,我认为没有必要重新上传它们。

那么,是否可以将我的上传者指向使用与我的 store_dir 不同的文件夹结构的现有图像?

这是我现在拥有的一个例子。

迁移示例块:

ActiveRecord::Base.connection.execute(update_images)

    Models::Product.where('image IS NOT NULL').select(:id, :image).find_in_batches(batch_size: 500).with_index do |group, batch|
      puts "Processing group ##{batch}"

      group.each do |prod|
        next unless prod[:image].present?

        image = Models::ProductImage.new(product_id: prod[:id])
        image.remote_image_url = prod[:image]
        image.save
      end

      sleep(7)
    end

上传者:

class ImageUploader < CarrierWave::Uploader::Base
  storage :fog

  def filename
    "#{secure_token}.#{file.extension}" if original_filename.present?
  end

  def store_dir
    "#{ENV['RACK_ENV']}/#{sub_folder}"
  end

  def cache_dir
    "#{ENV['RACK_ENV']}/#{sub_folder}"
  end

  def extension_allowlist
    %w(jpg jpeg gif png)
  end

  def size_range
    1.byte..10.megabytes
  end

  def sub_folder
    case model
    when Models::UserImage
      'user_images'
    when Models::ProductImage
      'product_images'
    else
      'misplaced'
    end
  end

  protected

  def secure_token
    var = :"@#{mounted_as}_secure_token"
    model.instance_variable_get(var) or model.instance_variable_set(var, SecureRandom.uuid)
  end
end

I'm trying to use existing images on my s3 and relate them to a carrierwave object. These images are stored in a different location from the store_dir of my uploader.

I currently have my migration setup using remote_image_url but it is extremely slow and I don't think re-uploading them is necessary.

So, is it possible to point my uploader to an existing image that uses a folder structure different from my store_dir?

Here's an example of what I have now.

example block from migration:

ActiveRecord::Base.connection.execute(update_images)

    Models::Product.where('image IS NOT NULL').select(:id, :image).find_in_batches(batch_size: 500).with_index do |group, batch|
      puts "Processing group ##{batch}"

      group.each do |prod|
        next unless prod[:image].present?

        image = Models::ProductImage.new(product_id: prod[:id])
        image.remote_image_url = prod[:image]
        image.save
      end

      sleep(7)
    end

uploader:

class ImageUploader < CarrierWave::Uploader::Base
  storage :fog

  def filename
    "#{secure_token}.#{file.extension}" if original_filename.present?
  end

  def store_dir
    "#{ENV['RACK_ENV']}/#{sub_folder}"
  end

  def cache_dir
    "#{ENV['RACK_ENV']}/#{sub_folder}"
  end

  def extension_allowlist
    %w(jpg jpeg gif png)
  end

  def size_range
    1.byte..10.megabytes
  end

  def sub_folder
    case model
    when Models::UserImage
      'user_images'
    when Models::ProductImage
      'product_images'
    else
      'misplaced'
    end
  end

  protected

  def secure_token
    var = :"@#{mounted_as}_secure_token"
    model.instance_variable_get(var) or model.instance_variable_set(var, SecureRandom.uuid)
  end
end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文