Amazon S3 for Your Ruby-on-Rails App
With the increased application penetration into the clouds, users may face the issue of keeping data in a cloud storage. One of the most popular solutions in this area is Amazon Simple Storage Service (Amazon S3).
Getting started with Amazon S3
First, we need to include support for Amazon S3 in the Amazon Web Services (AWS) account.
Create a bucket for your project, but remember that a name must be unique across all Amazon S3 accounts. Then, you are able to manage access permissions to your bucket in the Properties tab.
After this, we can go to our Ruby-on-Rails 3.0 application and start configuring it to interact with Amazon S3. Most of Ruby-on-Rails 3.0 gems that support cloud storages use the fog library.
Configuring CarrierWave uploader
One of the popular uploaders—CarrierWave—has a description of how to store uploads on Amazon S3. You can initialize a storage type in the
You can do the same in
Now, we have an issue with the development and test environments. We don’t need to use Amazon S3 in these environments. I suggest that we do a simple trick: put a storage type into your config file and use
Another useful gem that will help us to store assets on Amazon S3 is Asset Sync. To configure this gem, we should tell it a path where we want to store assets.
The default matcher for compiling files includes
To include specific files, you should put them to a precompile config.
To include all your asset files, you should put the following regular expression to config.
We should also configure
asset_sync. We can generate a default config file by executing the Rake task.
Then, we should put the Amazon S3 config information in it.
In addition, we can see the region option in both gems. It is highly recomended to configure it correctly to increase upload performance. Now, when we call
assets:precompile, assets will be uploaded to a specified Amazon S3 bucket.
So, these steps should be enough to basically configure your app to work with Amazon S3.
- Organizing Storage in Multiple Fog Containers Using CarrierWave
- Planning Your Cloud Stack: Storage and Database Solutions
- Using IBM Bluemix Object Storage in Ruby Projects
About the author
Nikolai Sharangovich is an experienced Ruby on Rails developer with a deep understanding of the object-oriented design and modern software principles. He likes to collaborate with product people to achieve a maximum impact. Find him on GitHub.