#native_company# #native_desc#
#native_cta#

Taking Advantage of Simple Cloud with the Zend Cloud Component Page 2

By W. Jason Gilmore
on January 24, 2011

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.