Rackspace Cloudfiles PHP API

If you need to use the Rackspace (formerly Mosso) Cloudfiles, but don't like their PHP API, we've developed a PHP API that works with the Zend Framework and is modeled after Zend Service Amazon S3 to have a similar interface.

There are a variety of options to all of these functions that are well documented in the code. The most basic examples are shown here.

Requirements

You'll need PHP 5.2.4 and the Zend Framework in order to use this API.

Usage

Setting up the Environment

You can either use an autoloader to load the necessary files, or load them manually:

  1. <?php
  2. require_once('Compass/Service/Rackspace/Cloudfiles');
  3. /* Initiate the class. */
  4. $cf = new Compass_Service_Rackspace_Cloudfiles('username', 'api_key');
  5. $cf->auth();
  6. ?>

Creating Containers and CDN Enabling

  1. <?php
  2. /* Create a container called media.example.com and CDN enable it */
  3. $cf->createContainer('media.example.com');
  4. $cf->cdnEnableContainer('media.example.com');
  5. ?>

Getting a list of Containers

  1. <?php
  2. $containers = $cf->getContainers();
  3. foreach ($containers as $container) {
  4. echo '-- I have container ' . $container . '<br />';
  5. }
  6. ?>

Get a list of objects in a Container

  1. <?php
  2. $items = $cf->getObjectsByContainer('media.example.com');
  3. print_r($items);
  4. ?>

Emptying a Container

  1. <?php
  2. $cf->cleanContainer('oldcontainer.example.com');
  3. ?>

Removing a Container

  1. <?php
  2. $cf->removeContainer('oldcontainer.example.com');
  3. ?>

Upload file contents to Cloudfiles

If you have the contents of a file loaded in a variable, you can upload that to Cloudfiles. You can also use the example below if you want to "copy" a file from your filesystem to Cloudfiles.

  1. <?php
  2. /* Load contents of the file into a variable */
  3. $contents = file_get_contents('/path/to/file/assets/images/file.jpg');
  4. /* Put the object on Cloudfiles with the name assets/images/file.jpg */
  5. /* Note: MUST prefix your filename with the name of your container */
  6. $cf->putObject('media.example.com/assets/images/file.jpg', $contents);
  7. ?>

Copy a file to Cloudfiles

If the file is on your filesystem, the example above can be simplified to:

  1. <?php
  2. /* Full path to file as your first parameter */
  3. /* Container name plus desired Cloudfiles filename as second parameter */
  4. $cf->putFile('/path/to/file/assets/images/file.jpg',
  5. 'media.example.com/assets/images/file.jpg');
  6. ?>

Removing a file from Cloudfiles

To get rid of the file we just uploaded in the last step:

  1. <?php
  2. $cf->removeObject('media.example.com/assets/images/file.jpg');
  3. ?>

Download

You can download the zip file here. Although the code works great for us, be advised you are using at your own risk and we are not responsible for any damages/problems that may arise from your use. Leave a comment below if you have any questions.

Comments

 

We've taken a chore and made it enjoyable

Our customers tell us that they actually enjoy updating their websites with Compass. Find out why. Schedule a one-on-one, no-obligation demo to see Compass in action.

Small to Midsize Businesses • Non-Profits • Organizations • Professional Service Firms

Testimonial CFO Selections

Tom Varga
Managing Partner, CFO Selections

    "Compass is a must have application for any business. It's brilliantly crafted and significantly
                  better then what has been previously available on the market to manage your website."

Hosted Web Content Management System CMS

We offer a fully managed Software as a Service (SaaS) solution that lets you focus on running a great website - not managing servers and software code. Learn More

Our Usability Principles

Compass CMS is built around the principle that software should be easy to use. Here's what that means to us. Learn More

Sign up for our WCM email newsletter

Our email newsletter contains information on web content management (WCM), usability, website best practices, SaaS, and other free resources.

Powered By Compass