Windows Azure Storage for PHP Developers
Consumer web applications today need to be web-scale to handle the increasing traffic requirements. One of the key components of the contemporary web application architecture is storage. Social networking and media sites are dealing with massive user generated content that is growing disproportionately. This is where Cloud storage can help. By leveraging the Cloud storage effectively, applications can scale better.
Windows Azure Storage has multiple offerings in the form of Blob, Table and Queues. Blobs are used to store binary objects like images, videos and other static assets while Table is used for dealing with the flexible, schema-less entities. Queues allow asynchronous communication across the role instances and components of Cloud applications.
In this article, we will see how PHP developers can talk to Azure Blob storage to store and retrieve objects. Before that, let’s understand the key concepts of Azure Blobs.
Azure Blobs are a part of Windows Azure Storage accounts. If you have a valid Windows Azure account, you can create a storage account to gain access to the Blobs, Tables and Queues. Storage accounts come with a unique name that will act as a DNS name and storage access key that is required for authentication. Storage account will be associated with the REST endpoints of Blobs, Tables and Queues. For example, when I created a Storage account named janakiramm, the Blob endpoint is http://janakiramm.blob.core.windows.net. This endpoint will be used to perform Blob operations like creating and deleting containers and uploading objects.
Azure Blobs are easy to deal with. The only concepts you need to understand are containers, objects and permissions. Containers act as buckets to store binary objects. Since Containers are accessible over the public internet, the names should be DNS compliant. Every Container that is created is accessible through a scheme that looks like http://.blob.core.windows.net/.Containers group multiple objects together. You can add metadata to the Container which can be up to 8KB in size.
If you do not have the Windows Azure SDK for PHP installed, you can get it from the PHP Developer Center of Windows Azure portal. Follow the instructions and set it up on your development machine.
Let’s take a look at the code to create a Container from PHP.
Start by setting a reference to the PHP SDK with the following statement.
Create an instance of Storage Client with the access key.
$storageClient = new Microsoft_WindowsAzure_Storage_Blob('blob.core.windows.net', '', '');
To list all the containers and print the names, we will use the following code
for ( $counter = 0; $counter < count($containers); $counter += 1)
Uploading a file to the Blob storage is simple.
To list all the blobs in a container, just call listBlobs() method.
for ( $counter = 0; $counter < count($blobs); $counter += 1)
Finally, you can delete the blob by passing the container name and the key.
PHP Developers can consume Windows Azure storage to take advantage of the scalable, pay-as-you-go storage service.
- Janakiram MSV, Chief Editor, CloudStory.in