A Cloud Storage Server in PHP

Using AWS Services in Web and Mobile Applications

Web and mobile applications often use Amazon Web Services (AWS) database and storage services like:

  • DynamoDB - NoSQL - keyed access
  • SimpleDB - NoSQL - keyed access + SQL
  • S3 - objects (files)

The AWS PHP SDK calls for these services yields nested structures of data that are difficult to manipulate in PHP. It is best to encapsulate AWS into a storage server that our applications will use. Such a storage server should provide a REST interface to make it useful for applications.

Code Igniter

Building the storage server in PHP is too much work. It is better to implement it with Code Igniter, a PHP MVC framework.

Code Igniter has all we need: models, and controllers. Our applications will address our controllers to store/fetch AWS data. The AWS database and storage services will be represented as models.

Code Igniter does not provide a REST by default. We need to use the Code Igniter REST Server by Phil Sturgeon. Integrating the library is a simple matter of dragging three files into the project. More details about the integration are in the RESTful Services by Phil Sturgeon.

REST Server

Create a single controller inheriting from REST_Controller provided by the REST library. Create a function for each object in the application, say, workspace_post, document_post and user_post, for workspaces, documents, and users, respectively. This will obtain their parameters in the standard Code Igniter way with:

 $this->post('param');

GET is similar, with workspace_get, document_get, and user_get. Setting the parameters format to JSON is done in the library config.php file.

Operations on any object may involve multiple AWS services, so create a function for each AWS service:

  • table_operation - DynamoDB
  • domain_operation - SimpleDB
  • bucket_operation - S3

These encapsulate the AWS services and obtain as parameters, something like:

 function table_operation($table_name, $action,           
     $item_id, $range_id, $item)

for DynamoDB, and similar for SimpleDB and S3.

AWS Models

Include the AWS PHP SDK in the libraries folder of the application, and write a Code Igniter model for each AWS service. For SimpleDB,

 function __construct()
 {

    parent::__construct();

      // connect to the SDK
       include_once($this->config-              
                   >item('sdb').'sdk.class.php');

       // set credentials
       include (APPPATH.'/config/aws.inc.php');
       include_once(APPPATH.'/config/sdb.php');

       $this->sdb = new AmazonSDB();    
       $this->sdb->set_region(AmazonSDB::REGION_EU_W1);

 }

More details examples are provided in articles by Glyn Rob for DynamoDB, and Prashant Sugand for S3.

Other Clouds

Other cloud infrastructure providers such as Rackspace have similar services to AWS. We can switch our server to any of these by replacing the operations layer - table_operation, domain_operation, and bucket_operation, and writing models for that cloud provider.

On-Permises

To use an on-permises infrastructure, we can provide models for files and MySQL and have the table_operation use the MySQL model and the bucket_operation use the files model.

A REST Client

So we have our REST storage server, and a web application as another Code Igniter application on the same or different server. How are we going to make those REST calls without dealing with the hustle of curl in PHP. There is a REST Client library by Phil Sturgeon. It is easier to integrate it with Sparks, the Code Igniter extensions platform.

To call the REST server through the REST client have in your application controller constructor:

 public function __construct()
{

        parent::__construct();
        $this>load->spark('restclient/2.1.0');    
        $this->rest->initialize(array('server' =>
         'http://www.example.com/restserver'));

   }

In your controller operations, to insert a document, for instance, use the REST client with:

 $response = $this->rest->post('document',

      array('action'=>'insert', 'object_name'=>'doc_one',
         'object' => {  
              'workspace_id'=>'workspace_one',        
            'workspace_range_id'=>'1340781212',
            'author'=>'johndoe' ,
            'storage_id'=>'78097',     
            'description'=>'Folders structure of bash
                      scripts for mapping app server'  
            }

      )

 );

Mobile Apps

A mobile app on IOS, Android, or PhoneGap, can make POST and GET calls to the REST server directly.