Things are getting cloudy - Storing paperclip images on Amazon S3 and Heroku

Another learning curve! Using Heroku for app deployment and storing attachements on S3.

Heroku setup and Commands

Install Heroku

gem install heroku

Add your public key to Heroku

heroku keys:add

Create App

heroku create --stack cedar

Upload/Deploy App

git push heroku master

Amazon S3 setup

Add to Gemfile

gem 'aws-s3', :require => 'aws/s3'

Local host - add to .bashrc

export S3_KEY=mykey
export S3_SECRET=mysecret

Heroku

$ cd myapp
$ heroku config:add S3_KEY=my_key S3_SECRET=my_secret

Setup code to read the variables (config/initializers/s3.rb)

AWS::S3::Base.establish_connection!(
  :access_key_id     => ENV['S3_KEY'],
  :secret_access_key => ENV['S3_SECRET']
)

Paperclip

class User < ActiveRecord::Base
  has_attached_file :photo,
    :storage => :s3,
    :bucket => 'mybucket',
    :s3_credentials => {
      :access_key_id => ENV['S3_KEY'],
      :secret_access_key => ENV['S3_SECRET']
    }
end

Referencing images

For simple references (e.g. image tags) use

http://s3.amazonaws.com/bucketname/filename

to reference the image/file. Needs to be used for file uploads etc using paperclip on Heroku

 

 

See http://devcenter.heroku.com/articles/s3 for more info

 

Post new comment

  • Syntax highlight code surrounded by the {syntaxhighlighter SPEC}...{/syntaxhighlighter} tags, where SPEC is a Syntaxhighlighter options string or "class="OPTIONS" title="the title".
  • Lines and paragraphs break automatically.

More information about formatting options