Windows Azure Storage for PHP Developers

By narayanam_jr|23rd Feb 2012
Clap Icon0 claps
  • +0
    Clap Icon
Share on
close
Clap Icon0 claps
  • +0
    Clap Icon
Share on
close
Share on
close

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.


Windows Azure Management Portal


Storage Account


Windows Azure Storage Endpoints

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.


PHP SDK

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.

[crayon lang="php"]

require_once 'Microsoft/WindowsAzure/Storage/Blob.php';

[/crayon]

Create an instance of Storage Client with the access key.

[crayon lang="php"]

$storageClient = new Microsoft_WindowsAzure_Storage_Blob('blob.core.windows.net', '', '');

[/crayon]

To list all the containers and print the names, we will use the following code

[crayon lang="php"]

$containers=$blob->listContainers();

for ( $counter = 0; $counter < count($containers); $counter += 1)

print($containers[$counter]->name)."n";

[/crayon]

Uploading a file to the Blob storage is simple.

[crayon lang="php"]

$obj=$blob->putBlob('','','');

print($obj->Url);

[/crayon]

To list all the blobs in a container, just call listBlobs() method.

[crayon lang="php"]

$blobs=$blob->listBlobs('');

for ( $counter = 0; $counter < count($blobs); $counter += 1)

print($blobs[$counter]->name)."n";

[/crayon]

Finally, you can delete the blob by passing the container name and the key.

[crayon lang="php"]

$blob.deleteBlob('','');

[/crayon]

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

Want to make your startup journey smooth? YS Education brings a comprehensive Funding Course, where you also get a chance to pitch your business plan to top investors. Click here to know more.