如何仅在生产中使用Cloudinary,并在开发中使用我的文件存储(使用Carrierwave Rails)?

时间:2018-12-13 20:10:02

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

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

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

我尝试使用{error,eopnotsupp}来解决此问题,但是没有用。有解决的办法吗?

我的上传者是:

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

谢谢!

1 个答案:

答案 0 :(得分:0)

我还记得在过去的项目中无法处理这种情况。在使用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

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