简体   繁体   中英

How can use Cloudinary just on production and use my file storage on development (Using Carrierwave Rails)?

I have an application Rails deployed on Heroku . I'm using the adds-on Cloudinary and the gem Carrierwave to upload images.

Following the Cloudinary documentation I have could configure the uploading images on Production successfully but in Development, it is also uploading to the cloud. My issue here is that I would like to use the file storage in my local machine instead of upload images to Cloudinary (just upload to Cloudinary in production).

I have tried to fix that using storage :file if Rails.env == "development" but didn't work. Any idea to solve it?

My uploader is:

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

Thanks!

I also remember not being able to handle this situation in a past project. As we were using AWS S3 we could differentiate assets from deve and prod by storing them in a different bucket, however in Cloudinary this seems to be not possible.

Maybe this setup we used for test environment might help you in your dev environment.

Place this code in 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

Hope this helps you. It worked for us.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM