Ruby on Rails, Paperclip, Amazon AWS S3 & Heroku
Had this problem before!
Solved it by putting the bucket vars in the model itself (live code):
#app/models/image.rb
has_attached_file :image,
:styles => { :medium => "x300", :thumb => "x100" },
:default_url => "**********",
:storage => :s3,
:bucket => '*****',
:s3_credentials => S3_CREDENTIALS
#config/application.rb
config.paperclip_defaults = {
:storage => :s3,
:s3_host_name => 's3-eu-west-1.amazonaws.com'
}
#config/initializers/s3.rb
if Rails.env == "production"
# set credentials from ENV hash
S3_CREDENTIALS = { :access_key_id => ENV['S3_KEY'], :secret_access_key => ENV['S3_SECRET'], :bucket => "*****"}
else
# get credentials from YML file
S3_CREDENTIALS = Rails.root.join("config/s3.yml")
end
#config/application.yml ([figaro][1] gem)
S3_KEY: ********
S3_SECRET: **********
We also have this in our production.rb:
#app/environments/production.rb
config.action_controller.asset_host = "//#{ENV['FOG_DIRECTORY']}.s3.amazonaws.com"
The ENV['FOG_DIRECTORY']
is the bucket name, and there's also one for different regions. Here's a very good resource for you (the answer with 15 upvotes... not the accepted answer)
Paperclip Amazon S3 setup with Heroku
The 'path' specifies the location on S3 where the files will be stored. Thus, if you specify an attachment as:
has_attached_file :image,
:styles => { :medium => "275x275>", :thumb => "175x155>" },
:storage => :s3, :s3_credentials => "#{RAILS_ROOT}/config/amazon_s3.yml",
:path => "user/:attachment/:style/:id.:extension"
A sample URL will be:
http://s3.amazonaws.com/bucket/user/image/thumb/347853856.jpg
Finally, S3 is NOT free (Heroku simply states transfer / uploads are not counted in the usage based calculations). Heroku's documentation is excellent if you need further information.
Paperclip + S3 + Heroku, but something went wrong
So, it was pretty simple.
It seems that Paperclip doesn't support more recent versions of aws-sdk. So I had to change my Gemfile in order to use aws-sdk 2.0 or older.
gem 'aws-sdk', '< 2.0'
That solved the problem.
Thanks for all the help. Using the environment variables of the AWS keys in development.rb helped a lot on debugging the code.
How to setup paperclip to upload multiple files to s3
I got it working!
I changed the model to the following and it started working again! I must have edited it at some point and put the wrong name after has_attached_file.
class Asset < ActiveRecord::Base
belongs_to :project
has_attached_file :asset,
styles: {
large: '640x480#',
medium: '300x300>',
thumb: '100x100#'
},
:path => ':class/:id/:style.:extension'
end
paperclip to amazon s3 images to heroku not working
Add the bucket name property to your heroku configuration by running the following command:
heroku config:add S3_BUCKET_NAME=bucket_name_value
For more information: Heroku: Config-Vars
Related Topics
I Can't Install Rmagick Gem on Windows
Why Does Openuri Treat Files Under 10Kb in Size as Stringio
What Is the Best Wysiwyg for Rails - Ruby on Rails Blog
In Ruby, How to Read Data Column Wise from a CSV File
Ruby: Write Escaped String to Yaml
Why Is Date.Today - 6.Months + 6.Months != Date.Today
Should I Learn MACruby or Rubycocoa
Ruby on Rails: If Current Page? Is Homepage, Don't Show Form
Why Do You Need "Require 'Bundler/Setup'"
Synchronized Method for Concurrency in Ruby
Mongoid: Find Through Array of Ids
Paperclip Amazon S3 Setup with Heroku
Spork 0.9.2 and Rspec 3.0.0 = Uninitialized Constant Rspec::Core::Commandline (Nameerror)
Dynamically Creating Class in Ruby
How to Alias a Class Method in Rails Model
Using Nokogiri HTML Builder to Create Fragment with Multiple Root Nodes