Using Rackspace Cloud Files for uploads with Phalcon

May 17th 2014

It will come as no surprise that I'm a really big fan of Rackspace, and their outstanding cloud services. This isn't about the provider, it's about Phalcon.

I use the heck out of the cloud storage on offer with Rackspace. If you haven't used it then you're missing out.

To get the PHP OpenCloud library, check out the v1.9.2 tag PHP OpenCloud - Release v1.9.2

To get using it in Phalcon, I've set up a simple service in DI to handle the connection, and the Cloud Files service.

The first thing you're going to need is to add the settings in your config file.

PHP Code:
<?php
return new \Phalcon\Config(
    array(
        
//
        // some configs
        //
        
'rackspace' => array(
            
'region' => 'LON'// Region, ex: LON or DFW or ORD
            
'container' => 'my-container-name',
            
'url' => 'http://12323532543423.234234234234.rackcdn.com'// The CDN Url (without the trailing /) to the container
            
'endpoint' => 'https://lon.identity.api.rackspacecloud.com/v2.0/'// Auth endpoint
            
'username' => ''// Username
            
'apiKey' => ''// API Key
        
)
        
//
        // some configs
        //
    
)
);

Now, presumably you've got a services file floating somewhere to construct your DI. If not, then the process is the same, just add this to your DI:

PHP Code:
<?php
// require your config, unless you already have it?
$config = require 'config.php';

$di = new \Phalcon\DI\FactoryDefault();

$di->set(
    
'rackspace',
    function () use (
$config) {
        
$rackspace = new \OpenCloud\Rackspace(
            
$config->rackspace->endpoint,
            array(
                
'username' => $config->rackspace->username,
                
'apiKey'   => $config->rackspace->apiKey
            
)
        );

        return 
$rackspace;
    }
);
$di->set(
    
'cloudFiles',
    function () use (
$config$di) {
        
$rackspace $di->get('rackspace');
        
$objectStoreService $rackspace->objectStoreService(null$config->rackspace->region);
        
$container $objectStoreService->getContainer($config->rackspace->container);

        return 
$container;
    }
);

Ok, so you've got a "rackspace" service and "cloudFiles" service set up.

I tend to use a BaseController for my MVC, but either way, you can use something like this to access your new "cloudStorage" service:

PHP Code:
<?php
    
/**
     * @return \OpenCloud\ObjectStore\Resource\Container
     */
    
protected function getRackspaceContainer()
    {
        return 
$this->cloudFiles;
    }

You'll notice that the "rackspace" service is completely separate from the "cloudFiles" service. Sure, there's a dependency on "rackspace" for "cloudFiles" but Rackspace have a load of REALLY awesome services you can utilise. Try out the servers API, or play with your DNS.

Clearly, you can enhance the example a bit. A good enhancement would be the caching of the credentials from Rackspace so you don't keep going through the authentication bit, but outside of that, it works!