How-to use the anynines Swift service with Carrierwave

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.

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 $ bundle
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.

.cfignore

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.

Deployment manifest

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.

There’s a great Railscasts episode on CarrierWave File Uploads. It also comes recommended to check out the Carrierwave wiki.

2 thoughts on “How-to use the anynines Swift service with Carrierwave

  1. Pingback: How to use the anynines Swift service with your app - anynines blog

  2. Pingback: Open Source Contribution - Fog OpenStack Adapter Public Urls

Leave a Reply

Your email address will not be published. Required fields are marked *