Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Configure Environment to Use Different Storage Paths on Amazon S3 with Carrierwave

I would like to have distinct folders in my S3 bucket to keep the production database clear from the development environment. I am not sure how to do this, here is the skeleton I've come up with in the carrierwave initializer:

if Rails.env.test? or Rails.env.development?
   CarrierWave.configure do |config|
     //configure dev storage path
   end
end

if Rails.production?
   CarrierWave.configure do |config|
     //configure prod storage path
   end
end
like image 568
PEF Avatar asked Aug 04 '11 07:08

PEF


1 Answers

Two options:

Option1: You don't care about organizing the files by model ID

In your carrierwave.rb initializer:

Rails.env.production? ? (primary_folder = "production") : (primary_folder = "test")

CarrierWave.configure do |config|
  # stores in either "production/..." or "test/..." folders
  config.store_dir = "#{primary_folder}/uploads/images"
end

Option 2: You DO care about organizing the files by model ID (i.e. user ID)

In your uploader file (i.e. image_uploader.rb within the uploaders directory):

class ImageUploader < CarrierWave::Uploader::Base

  ...

  # Override the directory where uploaded files will be stored.
  def store_dir
    Rails.env.production? ? (primary_folder = "production") : (primary_folder = "test")

    # stores in either "production/..." or "test/..." folders
    "#{primary_folder}/uploads/images/#{model.id}"
  end

  ...

end
like image 118
iwasrobbed Avatar answered Oct 26 '22 03:10

iwasrobbed