【问题标题】:CarrierWave not uploading (after upgrade)CarrierWave 未上传(升级后)
【发布时间】:2023-03-27 09:54:01
【问题描述】:

一个月前,我将 RoR-app 上的 Ruby 版本从 1.9.3 升级到了 2.3.3。在此之前,我已成功使用fogcarrierwave 将图像上传到 Amazon S3,但升级后它停止工作。它不再将我的图像上传到 S3,文件夹和文件不会在那里创建。造成这个问题的原因是我根本无法对其进行故障排除,我没有收到任何错误消息或任何东西。

版本:

  • 雾 1.38.0
  • 载波 1.0.0
  • 红宝石 2.3.3
  • 导轨 4.1.13

有用的信息:

  • 我尝试了雾/aws 和carrierwave-aws,结果相同(失败)
  • 如果我将 AWS 凭证更改为明显错误的内容,则会出现同样的问题。也就是说,CarrierWave 配置文件似乎有问题,或者它没有加载或其他原因
  • 对象(礼物)正在正确保存
  • 据我所知,我没有在 Amazon 上进行任何更改。
  • 我尝试切换到另一组 AWS 访问密钥和 ID,但遇到了同样的问题。
  • 我在同一个应用程序(使用相同的凭据)上使用 AssetSync 上传到 AWS,这很有效! IE。 AWS/S3 应该没有问题。

初始化程序/carrierwave.rb

CarrierWave.configure do |config|
  config.storage = 'fog'    
  config.fog_credentials = {
    :provider               => 'AWS',
    :aws_access_key_id      => 'ABC123CODE',
    :aws_secret_access_key  => '+ABC123CODEKEY', 
    :region                 => 'eu-central-1'
  }
  config.fog_directory  = 'myapp-se'
  config.cache_dir = "#{Rails.root}/tmp"

  config.fog_attributes = {
    expires: 1.year.from_now.httpdate,
    cache_control: "max-age=#{1.year.to_i}"
  }

  config.ignore_integrity_errors = false
  config.ignore_processing_errors = false
  config.ignore_download_errors = false

end

礼物模型(gift.rb)-(相关部分)

  mount_uploader :cached_image, GiftImageUploader
  #validates_integrity_of :cached_image
  #validates_processing_of :cached_image  
  validates_integrity_of :cached_image
  validates_processing_of :cached_image
  validates_download_of :cached_image 

    file = Tempfile.new(["image", ".jpg"])
    file.binmode

    begin 
      open(URI.parse(external_image_url), :allow_redirections => :safe) do |data|
        file.write data.read
      end           
    rescue Exception => e
      logger.info "Error fetching image for product #{id}: " + e.message
      return false
    end

    file.rewind

    self.cached_image = file
    begin
      self.skip_callbacks = true
      if self.save!
        logger.info "Successfully cached image for product #{id}"
        return true
      else
        logger.info "Soft Error saving product #{id}"
        return false
      end        
      self.skip_callbacks = false
    rescue Exception => e
      logger.info "Rescue Error saving product #{id}" + e.message
    end

上传文件

class GiftImageUploader < CarrierWave::Uploader::Base
  include CarrierWave::RMagick

  storage :fog

  def store_dir
    "cached/gifts/#{model.id}"
  end

  def extension_white_list
    %w(jpg jpeg gif png)
  end

  version :s300 do
    process resize_to_fit: [300, 300]
  end

  version :s200, :from_version => :s300 do
    process resize_to_fit: [200, 200]
  end

  version :s150, :from_version => :s200 do
    process resize_to_fit: [150, 150]
  end

  version :sq150 do 
    process resize_to_fill: [150, 150]    
  end  

  version :s75, :from_version => :s150 do
    process resize_to_fit: [75, 75]
  end

  version :sq75 do 
    process resize_to_fill: [75, 75]    
  end

end

我需要什么帮助

  • 由于我的主要问题是这一切都静默失败,我什至无法开始对该功能进行故障排除。非常感谢任何关于我如何尝试和错误地解决这个应用程序的问题,直到我能得到一些关于错误的信息的建议。
  • 如果您可以在代码中看到可以解决问题的任何内容,当然也会非常有用!

【问题讨论】:

  • 请调试代码并指出哪里出了问题。
  • 当我保存一个礼物对象时,以前这会导致在 S3 创建一个文件夹和文件。现在,当我保存一个礼品对象时,我输入了给我这个消息的代码部分:“产品#{id} 的成功缓存图像”。问题是我无法真正调试它,因为它只是静默失败。

标签: ruby-on-rails ruby amazon-s3 carrierwave fog


【解决方案1】:

我使用carrierwave-aws gem 解决了这个问题。对于我的特定应用程序而言,该解决方案太复杂且太具体,以至于未来的读者不会感兴趣。坦率地说,我不确定为什么这解决了这个问题。

【讨论】:

    【解决方案2】:

    您需要在启动应用程序之前运行bundle install 命令。如果它不起作用,请运行bundle update,然后运行bundle install。它应该可以工作,因为我过去也遇到过类似的问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-06-18
      • 2016-12-22
      • 2012-01-21
      • 1970-01-01
      • 2013-12-09
      • 2023-03-26
      • 2014-09-03
      • 2015-03-15
      相关资源
      最近更新 更多