Although still in their infancy, cloud-based services are already making an undeniable impact within businesses large and small. After all, the idea of reducing otherwise significant capital expenditures on hardware, software and service development by moving application services to a utility- or subscription-based model is pretty appealing, particularly so for small companies lacking the necessary manpower to manage these resources internally, or for startups who might need to rapidly provision new servers in order to meet sudden and growing demand for a popular new service.
The nebulous nature of the virtualized cloud infrastructure also makes it possible to migrate from one solution to another should a competing offering advertise a more attractive pricing structure or superior services to your currently chosen solution. At least that's the general idea. The challenge happens to reside within the client code, because the most commonly integrated cloud APIs are tied to a specific cloud service. This dilemma is apparent even within otherwise DRY-oriented framework solutions such as the Zend Framework, which offers substantial cloud support through a variety of different components, among them Zend_Service_Amazon_S3 and Zend_Service_WindowsAzure. Therefore, if you wanted to switch from S3 to Azure, you would need to refactor your code to use the Zend_Service_WindowsAzure-specific API.
In an effort to eliminate this inconvenience several companies (among them Zend, Microsoft, and IBM) have banded together to create the Simple Cloud API. The Simple Cloud API offers a unified interface for document storage, file storage, and simple queue services, and currently supports several of the most popular service vendors, including Amazon, Windows Azure, and Nirvanix. Zend has already moved quickly to incorporate support for Simple Cloud into the Zend Framework, greatly reducing the barriers and complexity involved in moving from one service to another, or using multiple vendors within the same application.

Introducing Zend_Cloud

Available since the 1.11 release, the Zend_Cloud component offers a unified interface to several of the aforementioned services. It works identically to other Zend Framework adapter-oriented components, requiring you to use a factory pattern to specify the desired concrete adapter class. From there you can use the unified interface to interact with the desired service. For instance, this is how you would use Zend_Cloud to instantiate the Amazon S3-specific adapter:
$storage = Zend_Cloud_StorageService_Factory::getAdapter(array(
  Zend_Cloud_StorageService_Factory::STORAGE_ADAPTER_KEY => 'Zend_Cloud_StorageService_Adapter_S3',
  Zend_Cloud_StorageService_Adapter_S3::AWS_ACCESS_KEY   => 'MY_ACCESS_KEY',
  Zend_Cloud_StorageService_Adapter_S3::AWS_SECRET_KEY   => 'MY_SECRET_ACCESS_KEY'
));
Of course, you'll need to swap out the AWS_ACCESS_KEY and AWS_SECRET_KEY placeholders with your respective keys. Further, rather than embed these keys within your controller actions, consider storing them in your application's application.ini file and referencing them within your actions as desired. See the article Introducing the Zend Framework's Application Configuration Component for more information about this useful feature.

Introducing Zend_Cloud (cont'd)

Once connected you can begin using the unified interface to interact with the S3 storage service. For instance, to store a text file you would use the $storage object's storeItem() method, like this:
...
$book = file_get_contents('/var/www/example.com/books/book.txt');
$result = $storage->storeItem(
  '/books/book.txt', 
  $data, 
  array(
    Zend_Cloud_StorageService_Adapter_S3::BUCKET_NAME => "wjgilmore"
  )
);
With the file stored on S3, you can later retrieve it like this:
$book = $storage->fetchItem("/books/book.txt", 
      array(Zend_Cloud_StorageService_Adapter_S3::BUCKET_NAME => "wjgilmore"));
Incidentally, it's possible to set a default bucket name within the factory, allowing you to forego passing the name to the storeItem() and fetchItem() methods. Of course, the greatest strength of the Simple Cloud API is the unified API. For instance, switching over from S3 to Windows Azure is done simply by refactoring the factory, as demonstrated here:
$storage = Zend_Cloud_StorageService_Factory::getAdapter(array(
  Zend_Cloud_StorageService_Factory::STORAGE_ADAPTER_KEY       => 'Zend_Cloud_StorageService_Adapter_WindowsAzure',
  Zend_Cloud_StorageService_Adapter_WindowsAzure::ACCOUNT_NAME => 'YOUR_ACCOUNT_NAME',
  Zend_Cloud_StorageService_Adapter_WindowsAzure::ACCOUNT_KEY  => 'YOUR_SECRET_KEY',
  Zend_Cloud_StorageService_Adapter_WindowsAzure::CONTAINER    => 'YOUR_CONTAINER',
  Zend_Cloud_StorageService_Adapter_WindowsAzure::HOST         => 'blob.core.windows.net'
));

$book = file_get_contents('/var/www/example.com/books/book.txt');
$result = $storage->storeItem(
  '/books/book.txt', 
  $data
);

Further Resources

Check out the following resources to learn more about the Simple Cloud API and the Zend_Cloud component:
Are you a Zend_Cloud early adopter? Tell us about your experiences in the comments.

About the Author

Jason Gilmore is founder of the publishing, training, and consulting firm WJGilmore.com. He is the author of several popular books, including "Easy PHP Websites with the Zend Framework", "Easy PayPal with PHP", and "Beginning PHP and MySQL, Fourth Edition". Follow him on Twitter at @wjgilmore.