05 Jun 2010

Rackspace Cloud Files API with Codeigniter

Development 12 Comments

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: , , , ,
  • Pete

    Fantastic! Thanks so much for the hard work. I’m in the process of implementing your Library now, but so far I’ve just plugged everything in as per the instructions and ran the “create_container” method and my new container has been created flawlessly!

    Thanks again.

    • http://www.modomediagroup.com Chris Gmyr

      Pete, I’m glad you like it so far and that it’s been helpful for your project. If you have any questions about anything, please feel free to let me know either through a comment, the contact form, or post it to Bit Bucket.

  • Pingback: CodeIgniter Rackspace Cloudfiles Library | Chris Gmyr

  • http://andrewmfraser.com Andrew M Fraser

    Thanks for your hard work! However I’m having trouble getting it to initialize, for some reason it doesn’t seem to like loading the library, I’m stumped and wouldn’t mind being pointed in the right direction if you have any ideas?

    My system is:
    Nginx + PHP 5.3.3 using CodeIgniter 2.0.3
    and a straight copy of your library files, config file and the controller into my application folder

    The only difference is I’ve edited the cloudfiles.php file and changed the default from US_AUTHURL to UK_AUTHURL.

    Thanks!

    • http://www.modomediagroup.com Chris Gmyr

      Hi Andrew,
      I’m sorry you are having trouble with setting this up on your system. Even though this was originally made in CI 1.7, there is nothing holding it back from working in 2.0. The only thing that I can think of is something in Rackspace’s library when you changed the auth URL. I know they have pushed out some updated to their API, so maybe they have fixed the issue.

      I will take a look at a few things and get back to you. I have your email address from your comment, so I can write you back directly.

      Thank you for stopping by and using my library. I will be in touch shortly.
      -Chris

      • http://andrewmfraser.com Andrew M Fraser

        Thanks Chris, the only error I could identify (only for CI 2.x) is that the Controller should be extends CI_Controller but when I try to initialize the library I just get a white screen like there is a syntax error, but unfortunately I can’t see one! :/

        Thanks for your help!

        • http://www.modomediagroup.com Chris Gmyr

          Hi Andrew, sorry for the delay. I’ve been pretty busy over here. I update the repo
          on BitBucket, so please download the new files. To use the UK URL you
          will have to edit: libraries/cf/cloudfiles.php -> line 137 and change
          the $auth_host to UK_AUTHURL. Please let me know how everything works out.

          Thanks,
          -Chris

          • http://andrewmfraser.com Andrew M Fraser

            Sorry been so busy, that’s working perfectly, thanks for your help it’s very much appreciated!

          • http://www.modomediagroup.com Chris Gmyr

            Glad it worked out for you! Please take a look at the repo again if you haven’t recently. I added exception/error handling that might be helpful for you.

  • CI_programmer

    container_objects in controller returns Invalid response (0): Container name not set even though i my container is there and i set container in constructor.

    Also whats the difference between $this->cf_container and $this->cf_folder in cfile.php?

    • http://www.modomediagroup.com Chris Gmyr

       Hello, I’ll check into the first part. I’m not sure why that is happening. As for the second part – Rackspace requires a container, but a “folder” is an option for you. Rackspace doesn’t support sub-containers, so you can make your own. If you container is “images”, then you want “thumbs” and “mains” folders, you can set the $this->cf_folder = “thumbs” (upload photos), then change to $this->cf_folder = “mains” (upload photos). These will go to: images/thumbs and images/mains respectively. You can also set as many levels as you want to a “folder” – thumbs/small/width_100/really_small … and so on

  • SASA Software Technologies

    Hi

    I am getting this error: Required Content-Type not set

    when i call cloudfiles/add_local_file