March 02, 2010 by Brandon Weiss
The biggest hurdle in my quest to get our site running on Heroku turned out to be asset uploading. Because of the way Heroku is architected, each application instance is read-only. You cannot just upload files and write them to disk, and even if you could, they would only be available to the instance you uploaded them to; none of the others would be able to see them.
Heroku is cloud-based hosting, so the solution is cloud-based storage. Any service will do, but I’d recommend Amazon S3. If you aren’t using it already, now is as good a time as any to switch. The benefits of having a fast, scalable storage system in the cloud are almost too many to count.
Unfortunately our site wasn’t using S3 yet, so the first step was to migrate all the uploaded assets to it. Luckily, if you’re using a file attachment library like Paperclip (which we are), that part is actually pretty easy.
Paperclip filesystem to Paperclip S3
To start, create an s3.yml and put it in config/. Here’s what’s in mine:
access_key_id: xxxxxxxxxxxxxxxxxxxx
secret_access_key: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
bucket: firebelly
If you like, you can also specify different accounts/buckets for development and production like you do for databases.
Then in your model change your attachment method from something like this:
has_attached_file :photo
To this:
has_attached_file :photo,
:storage => :s3,
:s3_credentials => "#{RAILS_ROOT}/config/s3.yml",
:path => ":attachment/:id/:style.:extension"
Then migrate the files themselves over to S3, and either change the structure to match your :path, or change the :path to match your directory structure. Be sure to set the access permissions correctly; you want Read set for all users, or the files won’t be viewable. Future file uploads will have permissions automatically set by Paperclip, but when you upload them manually the permissions must be set manually as well.
The Plot Thickens
I thought I was done, but then I saw it, lurking in the corner: attachment_fu. Some of our models were sadly still using attachment_fu, and while there’s nothing necessarily wrong with attachment_fu, Paperclip is just more modern and has become the de facto standard for file attachments. Plus, we definitely shouldn’t be using two different libraries in the same app; that’s just ugly. So I have to migrate all the models using attachment_fu to use Paperclip instead.
I had a feeling this was going to be really, really painful, so I hit up Google first to see if maybe someone else had written about it and I could save myself some trouble. But surprisingly, even though many people must have done this before me, I could only find one halfway decent guide for migrating from attachment_fu to Paperclip written by James Stewart. And just my luck, it didn’t work for me. I’m sure it must be something related to my particular setup, because it seemed to work for everyone in the comments, but I just got a bunch of errors that I couldn’t figure out. So instead I hacked James’ script to get around the errors. Here’s what I did:
Attachment_fu filesystem to Paperclip S3
First, grab a copy of the Paperclip migration script that I forked and modified (yay, GitHub) and put it in lib/.
Now make a new migration:
script/generate migration convert_project_slides
And edit it:
class ConvertProjectSlides < ActiveRecord::Migration include PaperclipMigrationsdef self.up add_paperclip_fields :project_slides, :photoProjectSlide.reset_column_informationProjectSlide.all.each do |project_slide| populate_paperclip_from_attachment_fu(project_slide, project_slide, 'photo', '/project_slides/') end enddef self.down remove_paperclip_fields :project_slides, :photo end end
Let me break down what’s happening in this migration.
- You have to include the
paperclip_migrations.rbscript in your migration. - Then the columns Paperclip uses need to be added to the
project_slidestable. - The arguments that have to be passed to
populate_paperclip_from_attachment_fuis where I got a little confused. The first one is the model you’re sending to, the second is the attachment you’re sending from. In this case they’re the same thing. The third one is the prefix you specified inadd_paperclip_fields, and the fourth is the directory where the attachment_fu files are.
The original script didn’t have that fourth argument because it can be gotten from attachment_fu, but inexplicably that wouldn’t work for me, so I had to disable attachment_fu and specify it manually.
But before you run the migration, you have to take out attachment_fu’s has_attachment declaration in your model. Now would also be a good time to replace it with Paperclip’s declaration. So just like before, something like this:
has_attachment :content_type => :image,
:storage => :file_system,
:path_prefix => 'public/project_slides',
:resize_to => '345x285>',
:thumbnails => { :thumb => '135x135!' }
Becomes something like this:
has_attached_file :photo,
:styles => { :thumb => '135x135!' },
:storage => :s3,
:s3_credentials => "#{RAILS_ROOT}/config/s3.yml",
:path => ":attachment/:id/:style.:extension"
One more thing before you run the migration. When attachment_fu creates thumbnails I guess the way it links them back to the original is by creating a new record in the table, and referencing the original record in a parent_id column, creating a tree structure. But I only want the migration to run on the original attachments, not thumbnails, so I deleted them from the database with something like:
DELETE FROM project_slides WHERE parent_id IS NOT NULL
That SQL query will delete any rows that have a value in the parent_id field (which means they aren’t originals).
OK, all set, now you can run the migration:
rake db:migrate
Assuming everything worked, you might want to drop attachment_fu’s various columns from your table. The ones I had were:
size
content_type
filename
height
width
parent_id
thumbnail
Lastly, just upload the photos to S3 like before, set the permissions and you’re done.
recent comments
categories
- Accessibility (2)
- Advertising (6)
- Book Review (5)
- Business (14)
- Camp (4)
- Community (32)
- Design (43)
- Development (26)
- Environmental (17)
- Exhbition (4)
- Fashion (2)
- Film Review (4)
- Film + Video (8)
- Food (1)
- Fun (4)
- Games (3)
- Geocoding (2)
- Health (5)
- Icon Design (1)
- Illustration (2)
- Life (14)
- Motion (7)
- Music (2)
- Philanthropy (16)
- Photography (2)
- Resources (21)
- Shopping (5)
- Socio-Political (32)
- Technology (14)
- Tutorials (10)
- Typography (11)
- Volunteer (4)
- Web Design (18)
archives
- June 2010 (1)
- March 2010 (1)
- February 2010 (1)
- January 2010 (2)
- November 2009 (9)
- October 2009 (14)
- September 2009 (7)
- August 2009 (9)
- July 2009 (11)
- June 2009 (7)
- May 2009 (10)
- April 2009 (9)
- March 2009 (16)
- February 2009 (15)
- January 2009 (14)
- December 2008 (9)
- November 2008 (17)
- October 2008 (11)
- September 2008 (1)
- August 2008 (9)
Twitter feed is temporarily down!

























