Tutorial - Using Blob Storage

Author: Maarten Balliauw

Date: Sunday, April 11, 2010, 12:00:00 AM

Tags: Tutorial, Storage

Table of Contents

Note:This article pertains to the CodePlex SDK initially released late 2009. The Windows Azure team has since then released a newer version of the Azure SDK for PHP on Github. Please refer to the Windows Azure PHP Developer Center for documentation on this more recent version of the SDK.

Please stay tuned and come back here regularly as we are working on refreshing the tutorials to deliver up to date and useful content for our PHP developers.

Recommended Reading

Synopsis

This tutorial will show the fundamentals of Windows Azure Table storage and provide a walk through that creates a working guest book application that can be deployed directly to Windows Azure.

Sample Files

This tutorial uses the GuestBookUsingBlobs sample application from the Windows Azure Sample Kit 4 PHP

Place a copy of the GuestBookUsingBlobs files in C:\temp for the remainder of this tutorial.

Thefile from GuestBookUsingBlobs which will be focused on in this tutorial is index.php.

Windows Azure blob storage fundamentals

Blob Storage stores sets of binary data. Blob storage offers the following three resources: the storage account, containers, and blobs. Within your storage account, containers provide a way to organize sets of blobs. A storage container can contain an unlimited amount of blobs, each of which can be up to 1 TB in size.

Addressing schema

A Windows Azure blob container is part of a storage account and is structured as follows: an account can contain zero or more containers which can contain zero or more blobs.

Blob containers are available on their specific HTTP(S) endpoint:

This endpoint is specific to the production Windows Azure environment. When working locally one can install the Windows Azure SDK and have access to a local, simulated storage environment. The endpoint for this environment is the following:

Blobs are available on their specific HTTP(S) endpoint:

Access to the storage endpoints is granted based on an endpoint that you choose and a generated account key. For the development storage environment the following account is the default one:

Account name (endpoint): devstoreaccount1
Account key: Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==

There is no need to remember the above: the Windows Azure SDK for PHP uses the above account name and account key by default when connecting to development storage.

clip_image001[1]

Figure 1 - Windows Azure blob structure

Architecture with blobs

Consider an online photo hosting service application as an example. Users can upload pictures to this service after which the pictures are resized into various sizes (thumbnail, small, medium, large, original). One would build this application architecture with one or more web servers in front, serving the actual web application and accepting uploaded pictures. A number of back-end servers would also be deployed to resize uploaded images into various sizes.

Where should photos be stored? Various alternatives are available:

  • Store photos on each webserver - Storage is not centralized, synchronization between servers should occur in order to have a copy of every photo on every server. Also, the maximum storage size is the size of the smallest web server hard disk if every server should contain every photo.
  • Store photos on one webserver - A viable option, however storage would not be duplicated and data loss is very likely if this server crashes.
  • Use a SAN / NAS storage solution - These solutions are often very expensive.

Windows Azure blob storage offers redundant, fast and easy-to-use storage which scales out when needed and is always available on one central endpoint for servers to connect.

clip_image002[1]

Figure 2 - Blob architecture

Using this architecture there are a number of advantages in using Windows Azure blob storage:

  1. Scalability - Whether the total amount of storage capacity used is 5 MB or 100 TB, Windows Azure blob storage will automatically and transparently scale out storage capacity.
  2. Reliability - Every blob is guaranteed to be replicated across storage nodes at least three times at all times. Whenever a storage node fails Windows Azure will automatically provision a new node and ensure data is replicated.
  3. Decoupling - Every machine in a Windows Azure solution can access blob storage through a central endpoint. There is no affinity between a server and a blob storage account.

Using the Windows Azure CDN

When an application is used from many locations around the world, using a CDN easily enables better performance and user experience for users who are farther from the source of the content stored in the Windows Azure Blob service. In addition, Windows Azure CDN provides worldwide high-bandwidth access to serve content for popular events.

Windows Azure CDN has locations globally (United States, Europe, Asia, Australia and South America) and continues to expand. It caches your Windows Azure blobs at strategically placed locations to provide maximum bandwidth for delivering your content to users. You can enable CDN delivery for any storage account via the Windows Azure Developer Portal.

Note that the CDN provides edge delivery only to blobs that are in public blob containers, which are available for anonymous access.

For more information on the Windows Azure CDN see the MSDN article Getting Started with the Windows Azure CDN.

Blob properties

After uploading a blob to storage a PHP object is created which contains several properties regarding the specific blob. This object is also created each time a blob is retrieved from storage. The following is a list of the available blob properties.

Container

Name of the container in which the blob is stored

Name

Name of the blob

SnapshotId

Only available if the blob is a snapshot (backup) of another blob

Etag

Entity tag, used for versioning and concurrency

LastModified

Timestamp of when the blob was last modified

Url

Full URL to the public location of the blob

Size

Size of the blob in bytes

ContentType

Content type header

ContentEncoding

Content encoding header

ContentLanguage

Content language header

CacheControl

Cache control header

BlobType

Blob type (block or page)

LeaseStatus

Blob leases status

IsPrefix

Is it a blob or a directory prefix?

Metadata

Key/value pairs of metadata


Connecting to Windows Azure blob storage

The Windows Azure SDK for PHP supports connecting to table storage both in the Windows Azure cloud and locally for development purposes.

Local storage

The Windows Azure development environment includes a local version of the Windows Azure storage server. When the Microsoft_WindowsAzure_Storage_Blob object is not provided connection details in the constructor it will automatically assume the developer wishes to connect to the local storage account. The following code is used to connect to the local storage blob.

$blob = new Microsoft_WindowsAzure_Storage_Blob();

Windows Azure cloud storage

When using the Windows Azure cloud storage the Microsoft_WindowsAzure_Storage_Blob object accepts three parameter in order as follows:

  1. Address to the storage blob server
  2. Storage account (endpoint)
  3. Storage account key

The following code will allow you to connect to a Windows Azure blob storage account

$blob = new Microsoft_WindowsAzure_Storage_Blob(
    'blob.core.windows.net',
    <storage account (endpoint)>,
    <storage account key>
);

Ensure a container exists be used

All blobs are stored inside of a container. There can be many containers with blobs inside of them, but each blob must belong to one container. When uploading a blob the container will not automatically be created if it does not already exist so it is a good idea to perform a quick, one line, check for the container. If it is not there it will be created.

$blob->createContainerIfNotExists(<name of container>);

The sample uses a container called guestbook, therefore the call will look like:

$blob->createContainerIfNotExists('guestbook');

Uploading a file to blob storage

Uploading a file to blob storage is fairly simple and involves one call with a minimum of the following three parameters in order:

  1. Name of container to place blob
  2. Name of the blob. This will be how it is referred to in URLs
  3. Local path to the blob file. Local to the machine the script is running on
$image = $blob->putBlob(<container>, <blob name>, <local file path>);

The sample uploads a file in the Image field. Because PHP knows the temporary location of the uploaded file it is simple to provide that path to putBlob and instantly transfer the file into blob storage.

$image = $blob->putBlob(BLOB_GUESTBOOK, $_FILES['Image']['name'], $_FILES['Image']['tmp_name']);

You may have noticed that the call to putBlob is setting the $image variable. After uploading a blob and object with the properties listed earlier will be returned. Now you are able to do things such as $blob->Url to find the full URL path to the blob.

Deleting a blob

Removing a blob from storage is as simple as providing the name of the container and the name of the blob to a single method.

$blob->deleteBlob(<container>, <blob name>);

Retrieving blobs

A single blob

A single blob may be retrieved if you know the container name and the blob name with the following:

$blob->getBlob(<container>, <blob name>);

List of blobs in a container

A list of all blobs in a container may be retrieved using the name of the container:

$blob->listBlobs(<container>);

Listing all containers

You can view a list of all containers by calling the listContainers method:

$blob->listContainers();

Run the sample

If you have been following along in the sample code you will have noticed that most of the methods listed in the tutorial have been implemented in order to present you with a complete sample that is ready to be run in your local Windows Azure development environment or in the Windows Azure cloud with very little change.

At this point you will want to run the sample in the local development environment. This can be easily done with the following:

  • Open a command line prompt
  • Type the command 'package create -in="C:\temp\GuestBookUsingBlobs" -out="C:\temp\GuestBookUsingBlobs\build" -dev=true'

After running this command your default web browser should open and the GuestBookUsingBlobs application will be presented to you, running on your local machine.

 
blog comments powered by Disqus