【问题标题】:How can use Cloudinary just on production and use my file storage on development (Using Carrierwave Rails)?如何仅在生产中使用 Cloudinary 并在开发中使用我的文件存储(使用 Carrierwave Rails)?
【发布时间】:2019-05-15 02:42:54
【问题描述】:

我在 Heroku 上部署了一个应用程序 Rails。我正在使用插件 Cloudinary 和 gem Carrierwave 来上传图片。

按照 Cloudinary 文档,我可以在生产环境中成功配置上传图像,但在开发环境中,它也在上传到云端。我的问题是我想使用本地机器中的文件存储,而不是将图像上传到 Cloudinary(只需在生产中上传到 Cloudinary)。

我尝试使用storage :file if Rails.env == "development" 解决此问题,但没有成功。有什么办法解决吗?

我的上传者是:

class ImageUploader < CarrierWave::Uploader::Base
  include Cloudinary::CarrierWave
  include CarrierWave::MiniMagick

  # storage :file if Rails.env == "development"

  def store_dir
    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
  end

  version :thumb do
    process resize_to_fit: [50, 50]
  end

  version :card do
    process resize_to_fit: [250, 250]
  end
end

谢谢!

【问题讨论】:

    标签: ruby-on-rails heroku carrierwave cloudinary uploader


    【解决方案1】:

    我还记得在过去的项目中无法处理这种情况。当我们使用 AWS S3 时,我们可以通过将资产与开发和生产区分开来,将它们存储在不同的存储桶中,但是在 Cloudinary 中这似乎是不可能的。

    也许我们用于测试环境的这个设置可能会对您的开发环境有所帮助。

    将此代码放入config/initializers/carrierwave.rb

    if Rails.env.test? || Rails.env.development?
      CarrierWave.configure do |config|
        config.storage = :file
        config.enable_processing = false
        config.asset_host = ActionController::Base.asset_host
      end
    
      ImageUploader
    
      CarrierWave::Uploader::Base.descendants.each do |klass|
        next if klass.anonymous?
        klass.class_eval do
          # Need to set the storage here otherwise the class
          # overrides it in the uploader definition
          storage :file
    
          def cache_dir
            "#{Rails.root}/spec/support/uploads/tmp"
          end
    
          def store_dir
            "#{Rails.root}/spec/support/uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
          end
        end
      end
    else
      CarrierWave.configure do |config|
        # Production setup
      end
    end
    

    希望这对您有所帮助。它对我们有用。

    【讨论】:

      猜你喜欢
      • 2012-12-20
      • 2011-11-14
      • 1970-01-01
      • 2019-08-06
      • 2013-10-31
      • 2015-03-21
      • 1970-01-01
      • 1970-01-01
      • 2018-12-23
      相关资源
      最近更新 更多