Using various S3 buckets for production and development at Carrierwave

I am starting to play with Carrierwave as an alternative to Paperclip .

I see from the documentation that in order to use S3 I have to configure Fog in the initializer:

CarrierWave.configure do |config| config.fog_credentials = { :provider => 'AWS', # required :aws_access_key_id => 'xxx', # required :aws_secret_access_key => 'yyy', # required :region => 'eu-west-1' # optional, defaults to 'us-east-1' } end 

However, how do I set up different buckets for different environments? With paperclip, I would specify different credentials and / or buckets for developing / producing / etc in the yml file. What is the best way to do this with a carrier?

+4
source share
2 answers

You could do it almost exactly the way you want it, like this completely untested idea:

 # config/initializers/carrierwave.rb CarrierWave.configure do |config| my_config = "#{Rails.root}/config/fog_credentials.yml" YAML.load_file(my_config)[Rails.env].each do |key, val| config.send("#{key}=", val) end end # config/fog_credentials.yml common: &common aws_access_key: 'whatever' ... fog_credentials: provider: 'whoever' ... production: <<: *common fog_directory: 'my-production-bucket' development: <<: *common fog_directory: 'my-dev-bucket' 

Or, if you want to abandon YAML, you can always simply test the environment in the initializer and use a case or conditional, in the simplest something like:

 CarrierWave.configure.do |config| if Rails.env.development? # configure one env else # configure another end # configure common stuff end 
+5
source
 class S3ArticleUploader < CarrierWave::Uploader::Base if Rails.env.test? storage :file else storage :fog end def fog_directory ARTICLE_UPLOADER_BUCKET end def store_dir "#{ model.parent_id }/#{ model.id }" end end # config/environments/development.rb ARTICLE_UPLOADER_BUCKET = 'development-articles' # config/environments/production.rb ARTICLE_UPLOADER_BUCKET = 'production-articles' 

calling the fog_directory method when you are not in TestEnvironment and initialize the BUCKET right.

You can also do the following:

  def store_dir if self._storage == CarrierWave::Storage::File "#{Rails.root}/tmp/files/#{ model.parent_id }/#{ model.id }" elsif self._storage == CarrierWave::Storage::Fog "#{ model.parent_id }/#{ model.id }" end end 

v2

 class S3ArticleUploader < CarrierWave::Uploader::Base if Rails.env.test? storage :file else storage :fog end def initialize self.fog_directory = ARTICLE_UPLOADER_BUCKET end def store_dir "#{ model.parent_id }/#{ model.id }" end end 
+1
source

All Articles