Skip to content

Amazon S3 and Paperclip on Rails 3

by dkberktas on September 14th, 2010

I previously posted about Using Amazon S3 with Paperclip. This post will talk about same topic. But this time for Rails 3.

  • Install aws-s3 by adding Gemfile
    gem 'aws-s3'

    and run

    bundle install
  • Create s3.yml file under config directory and enter your Amazon S3 credentials (also I assume you created the buckets for development and production)
    development:
      bucket: bucket-dev
      access_key_id: xxx
      secret_access_key: xxx
    test:
      bucket: bucket-test
      access_key_id: xxx
      secret_access_key: xxx
    production:
      bucket: bucket-pro
      access_key_id: xxx
      secret_access_key: xxx
  • Open your model file that will hold the attachment and modify it as follows:
      #paperclip
      has_attached_file :photo,
           :styles => {
           :thumb=> "100x100#",
           :small  => "400x400>" },
         :storage => :s3,
         :s3_credentials => "#{RAILS_ROOT}/config/s3.yml",
         :path => "/:style/:id/:filename"
  • That’s it! Simple isn’t it.

    links that might help

  • http://docs.heroku.com/s3
  • http://stackoverflow.com/questions/3055105/establishing-connection-w-amazon-s3-from-heroku
  • From → english, tech

    17 Comments
    1. safalmj permalink

      I’ve used Uploadify and paperclip in Rails3 and I’m getting this error -
      AWS::S3::NoSuchBucket (The specified bucket does not exist).

      My s3.yml file is
      ————————————————–
      development:
      bucket: bucket-dev
      access_key_id: ###
      secret_access_key: ###
      test:
      bucket: bucket-test
      access_key_id: ###
      secret_access_key: ###
      production:
      bucket: bucket-pro
      access_key_id:###
      secret_access_key: ###
      ————————————————–
      and the model has -
      ————————————————–
      has_attached_file :photo,
      :storage => :s3,
      :s3_credentials => “#{::Rails.root.to_s}/config/s3.yml”,
      :path => “/:style/:filename”,
      :styles => {:medium => “300×300>”,:thumb => “100×100>”}

      ————————————————–

      what should I do?

    2. Hello safalmj, you should create the buckets on Amazon S3 manually as far as I know, have you created the buckets (bucket-pro, bucket-test, bucket-dev)

    3. safalmj permalink

      Hi, dkberktas. Thanks a lot. Its working now. I used the bucket name that was previously created by my company.

    4. safalmj permalink

      Hi dkberktas. I have one more issue. My model looks like this –
      - – - – - – - – - – – - -
      has_attached_file :photo,
      :styles => {:medium => “300×300>”,:thumb => “100×100>”},
      :storage => :s3,
      :s3_credentials => “#{RAILS_ROOT}/config/s3.yml”,
      :path => “/:style/:id/:filename”
      - – - – - – – - – - – – -

      When I push my image file to Amazon S3, the file is saved inside three folders: (1) thumb, (2) original and (3) medium. I don’t want ‘original’ folder . I haven’t specified it inside model’s :styles{ } either. How do I prevent this ‘original’ folder from being saved in s3? Do I need to change any configuration with paperclip or model?

    5. I think only way to do it to define your own original style, so that paperclip override its own original. If you find any other way, just let me know.

    6. safalmj permalink

      Yeap… That seems to work. :-)

    7. mhoitomt permalink

      This was great, thanks for the quick overview! Rails and it’s community continue to amaze me

    8. Major props my man, this got me (a rails newbie) up and running in no time!

    9. Hesham permalink

      Thx, saved me at least a few hours. I got paperclip an S3 running under 5 minutes.

    10. won’t work for me… getting an error at the #{ROOT_PATH} hash. gah

    11. @JP: When using Rails 3.1.x use “#{Rails.root}/config/s3.yml” instead.

    12. Tony permalink

      Thank you, this was very helpful!

    13. Perfect! Thank you.

    14. Igor Alexandrov permalink

      I have created storage module for Paperclip that uses ‘aws-sdk’ gem. Works well, using it in production projects myself.
      https://github.com/igor-alexandrov/paperclip-aws

    15. Hello.

      I need to connect to the webservice of Amazon to get the info of books by ISBN. I have already signed up in Amazon S3, but i don`t know which gems I need and which is the name of the webservice.

      Do you know any note to do this? I understand this article is to store files in Amazon…

      Tnaks

    Trackbacks & Pingbacks

    1. Upload Files Directly to S3 with Plupload, Rails, and Paperclip | Atomic Spin
    2. How to get Paperclip and AWS-S3 Singapore (and European) buckets working | DC.

    Leave a Reply

    Note: XHTML is allowed. Your email address will never be published.

    Subscribe to this comment feed via RSS