Thursday, 18 December 2008

Using S3 With dm-paperclip

dm-paperclip is a port of Paperclip to Datamapper. It's a pretty straightforward port so most of the Paperclip documentation is valid for dm-paperclip. One of the most attractive features of Paperclip is it's ability to host your files on Amazon S3 instead of locally. Here's how you do it:

You will need 3 extra parameters on your has_attached_file declaration in your model:

has_attached_file :photo,
:storage => :s3,
:s3_credentials => Merb.root / "config" / "s3.yml",
:path => ':attachment/:id/:style.:extension'
validates_attachment_presence :photo
validates_attachment_size :photo, :in => 1..200000

The config/s3.yml file stores your S3 credentials and bucket name. This is very handy because you can have different configs and inheritance, just like database.yml:

development: &defaults
access_key_id: ...
secret_access_key: ...
bucket: project_images_development

production:
<<: *defaults bucket: project_images_production


That's it!

No comments: