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:
<?php
require_once('Compass/Service/Rackspace/Cloudfiles');
$cf = new Compass_Service_Rackspace_Cloudfiles('username', 'api_key');
$cf->auth();
?>
Creating Containers and CDN Enabling
<?php
$cf->createContainer('media.example.com');
$cf->cdnEnableContainer('media.example.com');
?>
Getting a list of Containers
<?php
$containers = $cf->getContainers();
foreach ($containers as $container) {
echo '-- I have container ' . $container . '<br />';
}
?>
Get a list of objects in a Container
<?php
$items = $cf->getObjectsByContainer('media.example.com');
print_r($items);
?>
Emptying a Container
<?php
$cf->cleanContainer('oldcontainer.example.com');
?>
Removing a Container
<?php
$cf->removeContainer('oldcontainer.example.com');
?>
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.
<?php
$contents = file_get_contents('/path/to/file/assets/images/file.jpg');
$cf->putObject('media.example.com/assets/images/file.jpg', $contents);
?>
Copy a file to Cloudfiles
If the file is on your filesystem, the example above can be simplified to:
<?php
$cf->putFile('/path/to/file/assets/images/file.jpg',
'media.example.com/assets/images/file.jpg');
?>
Removing a file from Cloudfiles
To get rid of the file we just uploaded in the last step:
<?php
$cf->removeObject('media.example.com/assets/images/file.jpg');
?>
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