One thing is usually true about developing any web application, it is always nice to have just a little bit more storage space. While developing Sagebit’s PICasso application, I realized that there are times when it is nice to have storage space not on your application and development machine. This is especially true if you expect to be handling large file sizes (e.g. high-resolution pictures or video files). Thankfully Ruby on Rails developers have a pretty convenient option at their disposal, Amazon Simple Service Storage a.k.a Amazon S3.
What is great about Amazon S3 is that it provides reliable data storage service that not only assists scalability of your Rails web applications, but it is also very cheap. In addition to this service being very cost-effective, it supports REST which any good Rails developer will tell you is great. And perhaps most impressive about Amazon S3 is how easy it is to actually integrate into your web application.
First create an Amazon Web Services account, and sign up to use S3 on Amazon. You will be given an access key id and a secret access key for your account. Then you create a bucket…yes a bucket. A bucket is like a folder that will hold all your files. For managing buckets for your S3 account, it is best to use a GUI application. Being an OS X Rails developer I would reccomend the S3 Browser. There are other S3 management applications out there for other operating systems too. You might also want to check out the Firefox addon . I would however, recommend, that you use a standalone application.If your using the S3 Browser, you just need to create a New Connection by entering you access key id and secret access key that you got when you created your Amazon account. There will be a similar option for other S3 GUI’s. You now will have the ability to manage buckets (e.g. delete, create, move, etc.) using the GUI application.
Now, time to integerate Amazon S3 into your Rails application. You first need to install the S3 gem:
sudo gem install aws-s3
Your Rails application also needs to know how to handle your Amazon S3 account specific to each environment. So will have to edit the amazon_s3.yml that gets created when you install the gem. Notice how similar it is to your database.yml.
All you have to do now is edit it. Enter your access key id and secret access key’s plus specify the bucket names:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
development: bucket_name: picasso_development access_key_id: xxxxxxx secret_access_key: xxxxxxxxxxxx test: bucket_name: picasso_test access_key_id: xxxxxx secret_access_key: xxxxxxxxxxxx production: bucket_name: picasso_production access_key_id: xxxxxx secret_access_key: xxxxxxxxxxxx |
What’s next…well that’s it! You’re done. You can now use your Amazon S3 buckets for storage. The great thing about Amazon S3 is that it has been widely accepted by the Rails community. So you will find plenty of plugins and applications that use this service. And so if you’re using attachment_fu to upload you picture files as we did in PICasso, you can tell attachment_fu to store your pictures on your S3 account simply by specifying this in your picture model:
has_attachment :content_type => :image, :storage => :s3, :size => 40.megabytes |
Attachment_fu is another useful and quite popular Rails plugin, but I won’t go into too much detail about it today…perhaps it will be a topic for another blog post.
So that was it, a simple integration of Amazon S3 for your Rails application. For me S3 relieves a burden of not having to worry about where my application’s data is and what state it is in. I find that keeping such application data separate form where the actual application is helps insure their safety.
In my next blog post I will discuss how we used the boling-for-batches plugin, written by Sagebit’s own Rails Guru Peter Boling , to upload a very large set of pictures to our Amazon S3 buckets.
Leave a Comment