How can I set paperclip's storage mechanism based on the current Rails environment?

后端 未结 7 1371
野性不改
野性不改 2020-12-12 11:07

I have a rails application that has multiple models with paperclip attachments that are all uploaded to S3. This app also has a large test suite that is run quite often. Th

7条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-12 11:24

    I like Barry's suggestion better and there's nothing keeping you from setting the variable to a hash, that can then be merged with the paperclip options.

    In config/environments/development.rb and test.rb set something like

    PAPERCLIP_STORAGE_OPTIONS = {}
    

    And in config/environments/production.rb

    PAPERCLIP_STORAGE_OPTIONS = {:storage => :s3, 
                                   :s3_credentials => "#{Rails.root}/config/s3.yml",
                                   :path => "/:style/:filename"}
    

    Finally in your paperclip model:

    has_attached_file :image, {
        :styles => {:thumb => '50x50#', :original => '800x800>'}
    }.merge(PAPERCLIP_STORAGE_OPTIONS)
    

    Update: A similar approach was recently implemented in Paperclip for Rails 3.x apps. Environment specific settings can now be set with config.paperclip_defaults = {:storage => :s3, ...}.

提交回复
热议问题