CakePHP CloudFiles Plugin for Rackspace CDN
Please Support:
CakePHP CloudFiles Plugin
Info:
- Version: 1.1.0
- Requirements: CakePHP 2.x, PHP 5.x, cURL
- Docs: CakePHP CloudFiles Plugin API
- Article: CakePHP CloudFiles Plugin Discussion and Examples
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.
This plugin utilizes the php-cloudfiles class created by Rackspace.
Create a file app/Config/cloud_files.php with the following:
Example of this configuration file is in app/Plugin/CloudFiles/Config/cloud_files.php.default
You must first App::uses the library in whatever file you want near the top.
There is also a helper class to assist image and streaming retrieval
Delete a container on Rackspace, notice container must be empty.
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-opencloudInstall
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
- git clone git://github.com/webtechnick/CakePHP-CloudFiles-Plugin.git app/Plugin/CloudFiles
- cd app/Plugin/CloudFiles
- git submodule init
- 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-opencloudConfiguration
Ensure the plugin is loaded in app/Config/bootstrap.php by calling CakePlugin::load('CloudFiles');- //app/Config/bootstrap.php
- CakePlugin::load('CloudFiles');
Create a file app/Config/cloud_files.php with the following:
- //app/Config/cloud_files.php
- 'server' => 'US', //UK
- 'username' => 'your_username', //your username
- 'api_key' => 'API_KEY', //your api key
- )
- );
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.
- App::uses('CloudFiles','CloudFiles.Lib');
Upload a file to Rackspace
Uploads a local file to the specified container in Rackspace- $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- 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- CloudFiles::delete('image.jpg','container_name');
List files on Rackspace
List files in a specified container on Rackspace- App::uses('CloudFiles','CloudFiles.Lib');
- //Get all files in container
- $files = CloudFiles::ls('container_name');
- //Get files in subfolder
- 'path' => 'pictures/animals'
- ));
- //Get files starting with a prefix
- 'prefix' => 'cake'
- ));
- //Limit the files returned
- 'limit' => 10
- ));
- //Limit the files returned, starting at marker
- 'limit' => 10,
- 'marker' => 30
- ));
Public or Streaming URL of a file on Rackspace
Get the URL of an object in Rackspace (streaming or public)- $url = CloudFiles::url('image.jpg','container_name');
- $stream = CloudFiles::stream('movie.mov', 'container_name');
There is also a helper class to assist image and streaming retrieval
- //Some Controller
- //Some View
- echo $this->CloudFiles->image('image.jpg','container_name');
- echo $this->CloudFiles->stream('movie.mov', 'container_name');
- echo $this->CloudFiles->url('some_file.txt', 'container_name');
List containers on Rackspace
List all containers on Rackspace- //Get all containers
- $containers = CloudFiles::listContainers();
- //Limit the containers returned
- 'limit' => 2
- ));
- //Show only public containers
- 'only_public' => true
- ));
Create container on Rackspace
Created a container on Rackspace, defaults to public container (CDN)- $Container = CloudFiles::createContainer('css');
- //Create a non-public container
- $Container = CloudFiles::createContainer('no_public', false);
Delete a container on Rackspace
Delete a container on Rackspace, notice container must be empty.
- CloudFiles::deleteContainer('container_name');