Web apps often produce files such as images, videos or documents. For a long time it was standard to put those files into the filesystem – but as we mentioned in a recent post – today that is considered a bad practice. Instead we recommend using OpenStack Swift, a highly scalable solution that can serve up to hundreds of petabytes. What’s more is that Swift is redundant, allowing the service to survive the outage of one or multiple servers.
In the post I mentioned earlier, we looked at the Paperclip gem, and combined it with Swift to produce files. In this post we’ll look at Carrierwave. The Carrierwave gem provides a simple and extremely flexible way to upload files from Ruby applications. It works well with Rack based web applications, such as Ruby on Rails.
Table of Contents
Carrierwave + Swift demo application
We wrote a small application that demonstrates how Rails 3.2.13, Carrierwave, a9s Swift Service and PostgreSQL work together. You can find the example app on GitHub. This application is an adaptation of n0ne’s Rails-Carrierwave-jQuery-File-Upload application. You can use this application to do a test run on anyines.com.
The a9s_swift gem
The example application uses the a9s_swift gem which provides simple configuration of the anynines Swift service within your applications. The gem supports Paperclip, CarrierWave and Fog configuration out of the box. We just need to add the gem to the Gemfile and add an initializer for Carrierwave to connect Carrierwave to Swift.
If you’d like to dive deeper into the a9s_swift_gem feel free to browse the Utility class.
Install the example app
Clone the git repository
$ git clone https://github.com/anynines/rails4_carrierwave_example.git
$ cd rails4_carrierwave_example
Create a database and run migrations
$ rake db:create db:migrate
Run the rails server
$ bundle exec rails s
Access your web application in the browser via localhost:3000
Deploy the application to anynines
Install the a9s gem
$ gem install a9s
In addition we set the Ruby version to 2.0.0 and added the rails_12factor gem to adjust the application to the anynines PaaS environment.
The .cfignore file is used to exclude files and directories from the Cloud Foundry deployment process. Since we don’t want to upload our locally added images, logs and temporary files to the anynines servers, we added the according directories to the .cfignore file.
The application provides a manifest.yml example file which has to be configured to address your desired application identifier and route within the anynines system. Please copy the manifest.yml.example to app_root/manifest.yml and replace all occurrences of app_name with your desired application identifier.
Edit the deployment manifest
$ cp manifest.yml.example manifest.yml
$ vim manifest.yml
… and exchange all occurrences of your_app_name with your desired application identifier.
The Ruby buildpack is referenced within the provided manifest.yml.example. This buildpack is needed to support Rails 4 and Ruby 2.
Deploy the application to anynines.com
You can deploy the application to anynines by executing the cf push command from the applications root directory:
$ cf push
For further information on how to deploy applications to anynines please refer to our documentation.