Rackspace Cloud Files API with Codeigniter
In one of my latest projects I wanted to have all of my users’ media files stored on my Rackspace Cloud Files account. I ran into a lot of initial problems with the API on my localhost and also with some options within the API. I chatted with some of their tech support people and even submitted a ticket with no help since coding/programming is not covered with their tech support. So do they not support their own API? hmmm… Well anyway, I didn’t get too discouraged and moved forward with trying to solve my problems. Here are the steps to take in order to get everything working within Codeigniter.
1. Download the latest release of their API or you can use mine which is already altered (1.7.4). I will be using mine for these instructions.
2. Make sure you install all of the necessary PHP plugins (CURL, mbstring, etc – instructions in the original API file).
3. Upzip and put the “cf” folder in your system/application/libraries/ directory and move the controller.php to system/application/controllers and rename the file to cloudfiles.php
4. Open the cloudfiles.php controller, change the $file_location and the different file names. You will also want to comment out the last 2 functions before you run it for the first time.
5. Open the cfiles.php (Library) file and enter your API username and key.
6. Run the controller page through your web browser and you should be all set! If you don’t see anything you might have to turn on display_errors in your index file and check to make sure all of the PHP plugins are installed and working.
This is a very simple library for their API, I only made this for the main functions that I need. Feel free to add additional ones as you see fit. For the 2 main functions “do_container” and “do_object”, for the first variable use either “a” (add) or “d” (delete), and the script will do the rest. The only other main API function that I changed was “delete_object” in cloudfiles.php, I commented out lines 1494-1502. This throws an error if you are trying to delete an object that isn’t in the cloud. I didn’t really like this since if a user is trying to delete a reference out of a database they obviously don’t need it any more, so why stop the script if there is no good reason.
I hope this makes using Rackspace’s Cloud Files with Codeigniter easier for you. If you run into any problems or questions, please feel free to leave a comment or contact us.
9/30/2010 Edit: With one of my more recent projects I decided to start using the Metadata option with Cloud Files. In one of my cron jobs I had to retreive all of the objects from a container and update a local database with the data, since that is faster than pinging the API all of the time. By default, the metadata does NOT show in the “get_objects” call. After almost a week of back and fourth with Rackspace’s support team, and even trying to rework some of the API myself, we finally found the solution. In the cloudfiles.php file, update line 1441 to: $tmp = new CF_Object($this, $obj["name"], False, True); This will enable the API to get all of the header information for the objects that you are getting.
1/5/2011 Edit: I’ve moved everything over to BitBucket for easier code sharing. I will be maintaining it here: https://bitbucket.org/modomg/codeigniter-rackspace-cloudfiles/ if you would like anything changed, have questions, or help please enter it into BitBucket.
Tags: cloud, codeigniter, examples, library, rackspace
Syracuse Web Design, Website Development, Hosting, Offsite Data Backup
Pingback: CodeIgniter Rackspace Cloudfiles Library | Chris Gmyr