store images locally for development s3 for production Rails Paperclip
Asked Answered
H

3

21

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

upload.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.

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

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 ]
  }
Hatcher answered 15/11, 2011 at 5:17 Comment(0)
R
15

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

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.

Roentgen answered 15/11, 2011 at 5:47 Comment(1)
Thanks Jordan. I will do as you suggest and also combine the idea above so that I can get rid of the the unless statement.Hatcher
V
16

One more solution is to move the hash with params to constants, which will be defined in config/environments/*.rb files. Then you can just use

has_attached_file :proto, PAPERCLIP_STORAGE_OPTS

Using if/unless in model while defining methods is a bit messy I think

Valais answered 15/11, 2011 at 6:34 Comment(1)
WOw that is a great idea. Thanks. I don't know how to award this as I need to combine your answer with Jordan's.Hatcher
R
15

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

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.

Roentgen answered 15/11, 2011 at 5:47 Comment(1)
Thanks Jordan. I will do as you suggest and also combine the idea above so that I can get rid of the the unless statement.Hatcher
B
3

Why not modify paperclip default options in production.rb?

Add this to 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']
  }
})
Babe answered 4/8, 2016 at 18:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.