Setup for an uploader (carrierwave)

天大地大妈咪最大 提交于 2020-01-25 11:00:06

问题


I have an image uploader in place following a tutorial using the gems carrierwave and fog. Now I would like to add an additional uploader but am struggling.

I have generated the uploader (rails generate uploader name). In the model file I have mounted the uploader to the right column (mount_uploader :column_name, nameUploader). In the uploader itself I have set def extension_white_list and store_dir. Also I included (since in the tutorial I did the same):

if Rails.env.production?
  storage :fog
else
  storage :file
end

Now where I'm stuck is that I don't know where to set the specifications for fog. That is, where to specify the Amazon bucket it should upload to. In a carrier_wave initializer I already had the code below. But this code specifies where to upload to for the uploader I had already implemented. These specifications are different for this new uploader. Where/how should I include these specs for the new uploader?

if Rails.env.production?   
  CarrierWave.configure do |config|
    config.fog_credentials = {
      :provider              => 'AWS',
      :aws_access_key_id     => ENV['S3_ACCESS_KEY'],
      :aws_secret_access_key => ENV['S3_SECRET_KEY'],
      :region                => ENV['AWS_REGION']
    }
    config.fog_directory     =  ENV['S3_BUCKET']
  end 
end

回答1:


By looking at this wiki page it seems like it is possible to override the config for each uploader

class AvatarUploader < CarrierWave::Uploader::Base
  # Choose what kind of storage to use for this uploader:
  storage :fog

  # define some uploader specific configurations in the initializer
  # to override the global configuration
  def initialize(*)
    super

    self.fog_credentials = {
      :provider               => 'AWS',              # required
      :aws_access_key_id      => 'YOURAWSKEYID',     # required
      :aws_secret_access_key  => 'YOURAWSSECRET',    # required
    }
    self.fog_directory = "YOURBUCKET"
  end
end


来源:https://stackoverflow.com/questions/31589872/setup-for-an-uploader-carrierwave

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!