Integrate with IBM Softlayer Object Storage02 Mar 2016
IBM SoftLayer Object Storage is a service exactly like Amazon Simple Storage Service (Amazon S3). It provides storage layer so that we can persist our file securely on the cloud. I have a chance to work with IBM Softlayer Object Storage during integration with our Rails app. IBM Softlayer Object Storage is relatively new and there are little materials regarding how to do this integration. In this article, I will wlkthrough how we use IBM Softlayer Object Storage to store and retrieve file by making use of Rails and Carrierwave gem.
1. New Rails app, Carrierwave and Fog gem
Let’s start by creating a new Rails app by running the following command:
After rails project has been created, add the following gems to your Gemfile:
Run bundle install to install these new gems into the project:
2. Configure Carrierwave to use IBM Softlayer object storage
We would need to create a a file
initializers/carrierwave.rb to indicate a few parameters for the IBM Softlayer object storage credentials. From
initializers folder inside your Rails app, create
carrierwave.rb file with the following content:
As you can see, I am using
ENV variable to protect the credentials. These
SOFTLAYER_CONTAINER key in
ENV are populated by my
rbenv-vars. You would need to install
rbenv-vars plugin for your
and then create a file call
.rbenv-vars in this Rails folder with the following content. Note: Replace the parameter with your real value:
You must also need to add
.rbenv-vars to your
.gitignore to prevent this file for being committed, pushed and exposed to the world.
username: is supposed to be your login username of IBM Softlayer
api_key: is API Key (Password) when you click on the link as indicated in the screenshot below.
cluster: is your cluster code as indicated in the cluster listing screenshot below.
container: like Amazon S3, it is the bucket name (IBM Softlayer use the terminology Container).
3. Prepare Avatar Uploader for Carrierwave
First, generate an uploader for Carrierwave by issuing the following command:
Then replace the content of that file by the following:
4. Create a Rails model to use Avatar Uploader
Let’s create a user model by issuing the following command:
Then add the following line to
user.rb file to use the avatar uploader:
Then run the migration from your terminal:
5. Test uploading & retrieving user avatar
First, open rails console by issuing the following command from your terminal:
Trigger the following commands in console to see if it works:
As you can see, we can now store files and retrieve back both the thumbnail and the original URL. The URL is an expiring URL.
Finally, as usual, the source code of this article is made available on this github URL - https://github.com/jameshuynh/blog-codes/tree/master/ibm-softlayer-object-storage-demo. Enjoy and have fun integrating with IBM Softlayer Storage