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

后端 未结 7 1369
野性不改
野性不改 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:28

    How about this:

    1. Defaults are established in application.rb. The default storage of :filesystem is used, but the configuration for s3 is initialized
    2. Production.rb enables :s3 storage and changes the default path

    Application.rb

    config.paperclip_defaults = 
    {
      :hash_secret => "LongSecretString",
      :s3_protocol => "https",
      :s3_credentials => "#{Rails.root}/config/aws_config.yml",
      :styles => { 
        :original => "1024x1024>",
        :large => "600x600>", 
        :medium => "300x300>",
        :thumb => "100x100>" 
      }
    }
    

    Development.rb (uncomment this to try with s3 in development mode)

    # config.paperclip_defaults.merge!({
    #   :storage => :s3,
    #   :bucket => "mydevelopmentbucket",
    #   :path => ":hash.:extension"
    # })
    

    Production.rb:

    config.paperclip_defaults.merge!({
      :storage => :s3,
      :bucket => "myproductionbucket",
      :path => ":hash.:extension"
    })
    

    In your model:

    has_attached_file :avatar 
    

提交回复
热议问题