【问题标题】:store images locally for development s3 for production Rails Paperclip在本地存储图像用于开发 s3 用于生产 Rails Paperclip
【发布时间】:2011-12-29 05:32:46
【问题描述】:

我想将图像上传到我的本地计算机上进行开发,但将它们存储在我的 Amazon S3 帐户中以用于生产。

上传.rb

if Rails.env.development?
  has_attached_file :photo, :styles => { :thumb => '40x40#', :medium => '150x200>', :large => '300x300>'},
                            :convert_options => { :thumb => "-quality 92", :medium => "-quality 92", :large => "-quality 92"  },
                            :processors => [:cropper]
else
  has_attached_file :photo, :styles => { :thumb => '40x40#', :medium => '150x200>', :large => '300x300>'},
                            :convert_options => { :thumb => "-quality 92", :medium => "-quality 92", :large => "-quality 92"  },
                            :storage => :s3,
                            :s3_credentials => "#{RAILS_ROOT}/config/s3.yml",
                            :path => ":attachment/:id/:style.:extension",
                            :bucket => 'birthdaywall_uploads',
                            :processors => [:cropper]
end

这里有一些代码重复。 有没有办法在不重复代码的情况下编写这个。

这是解决方案,感谢下面的 Jordan 和 Andrey:

config/environments/development.rb

   PAPERCLIP_STORAGE_OPTS = {
     :styles => { :thumb => '40x40#', :medium => '150x200>', :large => '300x300>' },
     :convert_options => { :all => '-quality 92' },
     :processor       => [ :cropper ]
   }

config/environment/production.rb

  PAPERCLIP_STORAGE_OPTS = {
    :styles => { :thumb => '40x40#', :medium => '150x200>', :large => '300x300>' },
    :convert_options => { :all => '-quality 92' },
    :storage        => :s3,
    :s3_credentials => "#{RAILS_ROOT}/config/s3.yml",
    :path           => ':attachment/:id/:style.:extension',
    :bucket         => 'birthdaywall_uploads',
    :processor       => [ :cropper ]
  }

【问题讨论】:

    标签: ruby-on-rails-3 paperclip


    【解决方案1】:

    另一种解决方案是将带有参数的哈希移动到常量,这将在 config/environments/*.rb 文件中定义。然后你就可以使用

    has_attached_file :proto, PAPERCLIP_STORAGE_OPTS
    

    我认为在定义方法时在模型中使用 if/unless 有点混乱

    【讨论】:

    • 哇,这是个好主意。谢谢。我不知道如何奖励这个,因为我需要将你的答案与乔丹的结合起来。
    【解决方案2】:

    当然。试试这样的:

    paperclip_opts = {
      :styles => { :thumb => '40x40#', :medium => '150x200>', :large => '300x300>' },
      :convert_options => { :all => '-quality 92' },
      :processor       => [ :cropper ]
    }
    
    unless Rails.env.development?
      paperclip_opts.merge! :storage        => :s3,
                            :s3_credentials => "#{RAILS_ROOT}/config/s3.yml",
                            :path           => ':attachment/:id/:style.:extension',
                            :bucket         => 'birthdaywall_uploads',
    end
    
    has_attached_file :photo, paperclip_opts
    

    除了明显的unless/merge! 块外,还要注意:all 用于:convert_options,而不是三次指定相同的选项。

    【讨论】:

    • 谢谢乔丹。我会按照你的建议做,并结合上面的想法,这样我就可以摆脱除非声明。
    【解决方案3】:

    为什么不修改 production.rb 中的回形针默认选项?

    将此添加到 config/environments/production.rb:

    Paperclip::Attachment.default_options.merge!({
      :storage => :s3,
      :bucket => 'bucketname',
      :s3_credentials => {
        :access_key_id => ENV['S3_ACCESS_KEY'],
        :secret_access_key => ENV['S3_SECRET_KEY']
      }
    })
    

    【讨论】:

      猜你喜欢
      • 2013-06-24
      • 2013-04-19
      • 1970-01-01
      • 2013-11-25
      • 1970-01-01
      • 2017-08-17
      • 1970-01-01
      • 2020-12-17
      • 2018-06-28
      相关资源
      最近更新 更多