Downloads

Google Ad

CakePHP CloudFiles Plugin for Rackspace CDN

Date: Fri, Mar 30th 2012, 02:28 Author: nick Views: 75348 Comments share

CakePHP CloudFiles Plugin

Info:
Watch:
Get it:
  • Download Now
  • git clone git://github.com/webtechnick/CakePHP-CloudFiles-Plugin.git Plugin/CloudFiles
Easy interaction, file uploading/downloading/management of Rackspace's CDN service.

CakePHP, CloudFiles, and You -- The Perfect Threesome

I've had the fun task of creating an interface to the popular Rackspace Cloud Files service. If you don't know what that is, its a service that allows you to store unlimited amounts of data remotely and serve them on the fly. You only pay for what storage you use and how much bandwidth you use. It's extremely affordable and a great way to speed up your application by off-hosting your assets (browsers can only download 2 concurrent assets per domain). To read more about the service visit here: http://www.rackspace.com/cloud/cloud_hosting_products/files/

This plugin utilizes the php-cloudfiles class created by Rackspace.

Requirements

This plugin required CakePHP 2.x, PHP 5.x, and because it utilizes php-opencloud, any requirements defined by that package https://github.com/rackspace/php-opencloud

Install

Now that your sold on the service, You can install this plugin to help jump start your CDN management system. There are two ways to install the plugin:

Git Installation

  1. git clone git://github.com/webtechnick/CakePHP-CloudFiles-Plugin.git app/Plugin/CloudFiles
  2. cd app/Plugin/CloudFiles
  3. git submodule init
  4. git submodule update

Manual Installation

Download https://github.com/webtechnick/CakePHP-CloudFiles-Plugin plugin into app/Plugin/CloudFiles Download https://github.com/rackspace/php-opencloud into app/Plugin/CloudFiles/Vendor/php-opencloud

Configuration

Ensure the plugin is loaded in app/Config/bootstrap.php by calling CakePlugin::load('CloudFiles');
  1. //app/Config/bootstrap.php
  2. CakePlugin::load('CloudFiles');

Create a file app/Config/cloud_files.php with the following:
  1. //app/Config/cloud_files.php
  2. $config = array(
  3.   'CloudFiles' => array(
  4.     'server' => 'US', //UK
  5.     'username' => 'your_username', //your username
  6.     'api_key' => 'API_KEY', //your api key
  7.   )
  8. );

Example of this configuration file is in app/Plugin/CloudFiles/Config/cloud_files.php.default

Usage Examples

The majority of the plugin is in a static library called CloudFiles, some of you purists would say "Hey Nick, you're interacting with a remote service, this should be in a datasource!" And normally I would say you're correct, although a little rude. ;-) However, a datasource is normally coupled with a model and I wanted a simple class to be called statically and preform the REST functions I required. If I created a datasource I couldn't make it static, and it wasn't as portable as I wanted the class to be. Plus, in CakePHP 2.x a lot of the core classes are moving to Libraries instead for easy portability across your application -- I'm following the suit.

You must first App::uses the library in whatever file you want near the top.
  1. App::uses('CloudFiles','CloudFiles.Lib');

Upload a file to Rackspace

Uploads a local file to the specified container in Rackspace
  1. $cdn_url = CloudFiles::upload('/path/to/image.jpg','container_name');

Download a file from Rackspace

Download a remote file on Rackspace in a specific container to a local file
  1. CloudFiles::download('image.jpg', 'container_name', '/local/path/to/image.jpg');

Delete a file from Rackspace

Delete a file from a specific container on Rackspace
  1. CloudFiles::delete('image.jpg','container_name');

List files on Rackspace

List files in a specified container on Rackspace

  1. App::uses('CloudFiles','CloudFiles.Lib');
  2. //Get all files in container
  3. $files = CloudFiles::ls('container_name');
  4.  
  5. //Get files in subfolder
  6. $files = CloudFiles::ls('container_name', array(
  7.   'path' => 'pictures/animals'
  8. ));
  9.  
  10. //Get files starting with a prefix
  11. $files = CloudFiles::ls('container_name', array(
  12.   'prefix' => 'cake'
  13. ));
  14.  
  15. //Limit the files returned
  16. $files = CloudFiles::ls('container_name', array(
  17.   'limit' => 10
  18. ));
  19.  
  20. //Limit the files returned, starting at marker
  21. $files = CloudFiles::ls('container_name', array(
  22.   'limit' => 10,
  23.   'marker' => 30
  24. ));

Public or Streaming URL of a file on Rackspace

Get the URL of an object in Rackspace (streaming or public)
  1. $url = CloudFiles::url('image.jpg','container_name');
  2. $stream = CloudFiles::stream('movie.mov', 'container_name');

There is also a helper class to assist image and streaming retrieval
  1. //Some Controller
  2. public $helpers = array('CloudFiles.CloudFiles');
  1. //Some View
  2. echo $this->CloudFiles->image('image.jpg','container_name');
  3. echo $this->CloudFiles->stream('movie.mov', 'container_name');
  4. echo $this->CloudFiles->url('some_file.txt', 'container_name');

List containers on Rackspace

List all containers on Rackspace
  1. //Get all containers
  2. $containers = CloudFiles::listContainers();
  3. //Limit the containers returned
  4. $containers = CloudFiles::listContainers(array(
  5.   'limit' => 2
  6. ));
  7. //Show only public containers
  8. $containers = CloudFiles::listContainers(array(
  9.   'only_public' => true
  10. ));

Create container on Rackspace

Created a container on Rackspace, defaults to public container (CDN)
  1. $Container = CloudFiles::createContainer('css');
  2.  
  3. //Create a non-public container
  4. $Container = CloudFiles::createContainer('no_public', false);

Delete a container on Rackspace


Delete a container on Rackspace, notice container must be empty.
  1. CloudFiles::deleteContainer('container_name');

Enjoy!

As always, comments are appreciated. I hope you enjoy the plugin.