Putting Files on the Rackspace File Cloud

Recently, I’ve been playing around with Asp.Net MVC and I (along with some friends) am creating an Asp.Net MVC application that can access files from our website. This means that files must display on the site as well as provide a way to download/upload them. I won’t give away all the details of the project but I will say this:
  1. Sharing photos is one of the driving forces for our project. 
  2. The files are only image type files( jpg, etc)
  3. The files must be uploaded by the users and stored somewhere within our system.
  4.  We don’t necessarily know at this time how much storage space we need but we think that we will need a little at first but possibly a lot more down the road. This means that the storage of these files must be scalable.
  5. The uploading of files must be controlled by our system.

 Right off the bat we started our application and were uploading files to the web server with no problem.  We only did it this way to kind of prove our system model. We were just basically writing them to our web server file system. That was a problem for a couple of reasons.

  1. No scalability – that means that as our need for storage grows there is finite amount of space on our server  and we will eventually run out of space
  2. Separation of Concerns - It puts the storage responsibilities on our web server. We only want the web server to host the site but be troubled with the responsibility of storing files.
For these reasons, we started looking into storing files on the cloud. We are still not done with our research, but we have found one candidate service that can do just that – Rackspace File Cloud services. The Rackspace File Cloud allows you to upload files onto the cloud. Rackspace has fast data transfers and provides a way to reference images with URL’s once they are uploaded (using the CDN system).

Rackspace provides all the necessary services to create the containers and upload the files. Rackspace has a set of RESTful services to enable access. While RESTful services are good, at the programming level we need first-class C# components that allow us to perform the necessary functions.

This is where the Rackspace’s APIs come in handy. Rackspace provides an API that enables us to write C# code directly against those services. That API can be found at github/rackspace. The site provides enough documentation to get you up and running fast. Here are some facts about running this API.

1. Account Setup - You must have an account with Rackspace and have accessed the API key from their dashboard. You will need the key in combination with your Rackspace username to establish the connection to the File Cloud. Make sure to give Rackspace a phone number that you can be reached at. At first I gave them my work number but I wasn’t at work, so I had to wait a couple of days to finish setting up my account. If you already have a Rackspace account then you don’t need to set one up. You just need to setup access to their File Cloud.

2. API Documentation - You can find good examples of using the API on the github.com account. Don’t do like I did and read through their code to figure it out. I didn’t notice initially that their API examples were online all along. Do not make the mistake of trying to read their auto-generated documentation. That thing just lists the classes and their methods but no examples or explanations.

3. API Availability - You don’t have to use the RESTful services to access the File Cloud but of course if you need to it is an option. You can, instead, use their available APIs. In my case I used the C# API but they have other APIs available. As of this writing I noticed that the other APIs available were Ruby, Python, PHP and Java.

4. Storage Approach - Do familiarize yourself with their storage approach. Their storage approach is that of containers and items, which is similar to folders and files. However, unlike folders, containers cannot be nested. Rackspace provides the manual at their website. It’s a short read and very handy in understating their approach.

5. Public File Availability - Rackspace provides the option for your files to be publicly available by a configuration setting on their dashboard. In my case that is handy because I can embed tags in my application that can reference them. In our case our images will be water-marked so it does not matter if people download them directly. Your case may be different. And like I said this service can be used for any file type and file size with a max limit of 5GB/per file.

6. Cost -The costs is relatively low - $.15/GB/Month. There are costs associated with retrieving and pushing files and also with querying information from your File Repository, so be aware of those. Make sure to check out Rackspace’s website for the full cost list.

7. Stand Alone Access - You do not need to have any type of monthly recurring charges from Rackspace to use the File Cloud. With Rackspace you pay for what you use. This also means that you do not need to instantiate a cloud server or a cloud site, managed website or otherwise. In my short proof of concept I wrote all API access code with a Winforms prototype. I chose this approach because I just wanted to prove connectivity and communication (i.e. a simple file transfer). I would like to see what those charges are at the end of month. Currently they read $0.00. I guess because I have not hit the 1GB marker.

Here is the example of that code.

public FileActionResult PutFileOnCloud(string remoteItemName, string localFileName)
        {
            try
            {
                string rackSpaceUserName = "myusername"; //get from rackspace
                string apiKey = "NEEDS REAL KEY HERE";   //get apiKey from rackspace
                string containerName = "testingcontainer";

                Connection connection = new Connection(new UserCredentials(rackSpaceUserName, apiKey));

                PutStorageItem putStorageItem = new PutStorageItem(connection.StorageUrl, containerName, remoteItemName, localFileName);

                var response = new GenerateRequestByType().Submit(putStorageItem, connection.AuthToken);
                
                if(response.StatusCode != HttpStatusCode.Created)
                {
                    throw new Exception(string.Format("Upload failed {0}", response.StatusDescription));
                }

            }
            catch (Exception exception)
            {
                return new FileActionResult
                           {
                               Message = string.Format("Faile storage failed: {0}", exception.Message),
                               Success = false
                           };
            }

            return new FileActionResult
            {
                Message = "File storage succeded",
                Success = true
            };
            
            
        }

The approach that I am taking in the code above is simple.  Take a file from my local machine and push it to the cloud. The API provides other ways such as using streams, but for this example I decided to keep it simple.  On the cloud the file is an item and not necessarily referred to as a file, hence the variable remoteItem. First thing in the code above you will notice is the user name. That is the user name provided by Rackspace when you set up and account.  Also you will notice and API key.  The API key is used to establish the connection.  The key can be retrieved from your Rackspace dashboard.  You will also notice we need a container name.  The code above assumes you already created a container.  You can create a container in one of two ways: programatically or through the Rackspace account dashboard.  You must first establish your connection with the connection and credentials.  The connection auto configures some basic information, like your authorization token and your URL, and stores it in the Connection object.

You must pass the authorization token for any requests to the File Cloud.  The URL follows a very specific format and is configured by the Connection object based on your credentials.

The next thing you will notice is the PutStorageItem class/object.  Then you must new up a GenerateRequestByType instance and submit your request.  Then wait for the response.  The response comes back a status and you know if your request was succesful or not.  Getting a file is very similar but you use the GetStorageItem instead.  The API has other features such as progress events that can be used to show a progress status and such.

I hope that this quick or maybe not so quick post has provided you with the information to get started writing files to the Rackspace File Cloud. 

Comments

Fernando Zamora said…
I forgot mention that the Rackspace API provides additional capabilities such as querying containters, getting account information, retriving files based on specific parameters, retrieving files and creating new containers. Container and file numbers are unlimited but they do have certain restrictions such as container name length names and such.
Ricardo Sanchez said…
Great information, thanks for sharing. This comes handy and at a very good time since I have been looking at Rackspace and Amazon S3 myself.

One thing I suggest you look at is MaxCDN, you can have it in front of your file cloud storage solution, it will give you the performance you'll need once you start getting decent traffic to this website.
Fernando Zamora said…
Thanks Ricardo. I will look into that.
Anonymous said…
Hi Fernando,

I'm having a hard time understanding how this all comes together. I put the .dll file on my server in the lib/cloudfiles folder. I saw the code example, but I don't get how to call to it. I'm a beginner with this. Do you have an aspx page you can share with me?

Thanks!

Jude
Anonymous said…
Hi Fernando,

I'm having a hard time understanding how this all comes together. I put the .dll file on my server in the lib/cloudfiles folder. I saw the code example, but I don't get how to call to it. I'm a beginner with this. Do you have an aspx page you can share with me?

Thanks!

Jude
Fernando Zamora said…
Anonymous your best bet would be to look at the open source project on git and look at the UnitTests for examples of how to use the API.
Fernando Zamora said…
Hi Anonymous,

I am not sure where you are stuck in the process of all this. This post is somewhat dated and I am not certain what the update process is for putting files on the RackSpace File Cloud. I would recommend visiting their site and starting there. At the time of this writing you were required to have a Rackspace account and you were required to receive an authentication credentials from Rackspace. You then had configure some of this on your rackspace dashboard. My recommendation is to start there. Rackspace is not the only Content Deliver Network service. Just google the topic and I'm sure you can find other ones. I hope this can help you and put you on the right path.
Anonymous said…
Hello,

i want to upload file on rackspace from asp.net application. for this from where i get want "com.mosso.cloudfiles.dll" ?

Thanks.
chik cchaa said…
I have been meaning to post something like this on my blog and you have given me an idea. Cheers.
webcare360

Popular posts from this blog

Setting Up XnaMobileUnit to run unit tests in Windows Phone 7 XNA Games

Reasons to Stay Away from CSLA