本文介绍了在本地存储图像以用于开发 s3 用于生产 Rails Paperclip的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的本地机器上上传图像用于开发,但将它们存储在我的 Amazon S3 帐户中用于生产.

I want to upload images on my local machine for development but store them on my Amazon S3 account for production.

上传.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

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

There is some code repetition here.Is there a way to write this without code duplication.

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

Here is the solution Thanks big time to Jordan and Andrey below:

config/environments/development.rb

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

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 ]
  }

推荐答案

好的.尝试这样的事情:

Sure. Try something like this:

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 而不是指定相同的选项三次.

In addition to the obvious unless/merge! block, also note the use of :all for :convert_options instead of specifying an identical option three times.

这篇关于在本地存储图像以用于开发 s3 用于生产 Rails Paperclip的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 10:12