Zend_Service_WindowsAzureIntroductionWindows Azure is the name for Microsoft's Software + Services platform, an operating system in the cloud providing services for hosting, management, scalable storage with support for simple blobs, tables, and queues, as well as a management infrastructure for provisioning and geo-distribution of cloud-based services, and a development platform for the Azure Services layer. Installing the Windows Azure SDKThere are two development scenario's when working with Windows Azure.
The first case requires you to install the » Windows Azure SDK on your development machine. It is currently only available for Windows environments; progress is being made on a Java-based version of the SDK which can run on any platform. The latter case requires you to have an account at » Azure.com. API DocumentationThe Zend_Service_WindowsAzure class provides the PHP wrapper to the Windows Azure REST interface. Please consult the » REST documentation for detailed description of the service. You will need to be familiar with basic concepts in order to use this service. FeaturesZend_Service_WindowsAzure provides the following functionality:
ArchitectureZend_Service_WindowsAzure provides access to Windows Azure's storage, computation and management interfaces by abstracting the REST - XML interface Windows Azure provides into a simple PHP API. An application built using Zend_Service_WindowsAzure can access Windows Azure's features, no matter if it is hosted on the Windows Azure platform or on an in-premise web server. Zend_Service_WindowsAzure_Storage_BlobBlob 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 within your storage account. Blob Storage is offered by Windows Azure as a REST API which is wrapped by the Zend_Service_WindowsAzure_Storage_Blob class in order to provide a native PHP interface to the storage account. API ExamplesThis topic lists some examples of using the Zend_Service_WindowsAzure_Storage_Blob class. Other features are available in the download package, as well as a detailed API documentation of those features. Creating a storage containerUsing the following code, a blob storage container can be created on development storage. Example #1 Creating a storage container
Deleting a storage containerUsing the following code, a blob storage container can be removed from development storage. Example #2 Deleting a storage container
Storing a blobUsing the following code, a blob can be uploaded to a blob storage container on development storage. Note that the container has already been created before. Example #3 Storing a blob
Copying a blobUsing the following code, a blob can be copied from inside the storage account. The advantage of using this method is that the copy operation occurs in the Azure cloud and does not involve downloading the blob. Note that the container has already been created before. Example #4 Copying a blob
Downloading a blobUsing the following code, a blob can be downloaded from a blob storage container on development storage. Note that the container has already been created before and a blob has been uploaded. Example #5 Downloading a blob
Making a blob publicly availableBy default, blob storage containers on Windows Azure are protected from public viewing. If any user on the Internet should have access to a blob container, its ACL can be set to public. Note that this applies to a complete container and not to a single blob! Using the following code, blob storage container ACL can be set on development storage. Note that the container has already been created before. Example #6 Making a blob publicly available
Root container
Windows Azure Blob Storage provides support to work with a "root container".
This means that a blob can be stored in the root of your storage account,
i.e. In order to work with the root container, it should first be created using the createContainer() method, naming the container $root. All other operations on the root container should be issued with the container name set to $root. Blob storage stream wrapperThe Windows Azure SDK for PHP provides support for registering a blob storage client as a PHP file stream wrapper. The blob storage stream wrapper provides support for using regular file operations on Windows Azure Blob Storage. For example, one can open a file from Windows Azure Blob Storage with the fopen() function: Example #7 Example usage of blob storage stream wrapper In order to do this, the Windows Azure SDK for PHP blob storage client must be registered as a stream wrapper. This can be done by calling the registerStreamWrapper() method: Example #8 Registering the blob storage stream wrapper
To unregister the stream wrapper, the unregisterStreamWrapper() method can be used. Zend_Service_WindowsAzure_Storage_TableThe Table service offers structured storage in the form of tables. Table Storage is offered by Windows Azure as a REST API which is wrapped by the Zend_Service_WindowsAzure_Storage_Table class in order to provide a native PHP interface to the storage account. This topic lists some examples of using the Zend_Service_WindowsAzure_Storage_Table class. Other features are available in the download package, as well as a detailed API documentation of those features. Note that development table storage (in the Windows Azure SDK) does not support all features provided by the API. Therefore, the examples listed on this page are to be used on Windows Azure production table storage. Operations on tablesThis topic lists some samples of operations that can be executed on tables. Creating a tableUsing the following code, a table can be created on Windows Azure production table storage. Example #12 Creating a table
Listing all tablesUsing the following code, a list of all tables in Windows Azure production table storage can be queried. Example #13 Listing all tables
Operations on entitiesTables store data as collections of entities. Entities are similar to rows. An entity has a primary key and a set of properties. A property is a named, typed-value pair, similar to a column. The Table service does not enforce any schema for tables, so two entities in the same table may have different sets of properties. Developers may choose to enforce a schema on the client side. A table may contain any number of entities. Zend_Service_WindowsAzure_Storage_Table provides 2 ways of working with entities:
All examples will make use of the following enforced schema class. Example #14 Enforced schema used in samples
Note that if no schema class is passed into table storage methods, Zend_Service_WindowsAzure_Storage_Table automatically works with Zend_Service_WindowsAzure_Storage_DynamicTableEntity. Enforced schema entitiesTo enforce a schema on the client side using the Zend_Service_WindowsAzure_Storage_Table class, you can create a class which inherits Zend_Service_WindowsAzure_Storage_TableEntity. This class provides some basic functionality for the Zend_Service_WindowsAzure_Storage_Table class to work with a client-side schema. Base properties provided by Zend_Service_WindowsAzure_Storage_TableEntity are:
Here's a sample class inheriting Zend_Service_WindowsAzure_Storage_TableEntity: Example #15 Sample enforced schema class
The Zend_Service_WindowsAzure_Storage_Table class will map any class inherited from Zend_Service_WindowsAzure_Storage_TableEntity to Windows Azure table storage entities with the correct data type and property name. All there is to storing a property in Windows Azure is adding a docblock comment to a public property or public getter/setter, in the following format: Example #16 Enforced property
Let's see how to define a propety "Age" as an integer on Windows Azure table storage: Example #17 Sample enforced property
Note that a property does not necessarily have to be named the same on Windows Azure table storage. The Windows Azure table storage property name can be defined as well as the type. The following data types are supported:
No enforced schema entities (a.k.a. DynamicEntity)To use the Zend_Service_WindowsAzure_Storage_Table class without defining a schema, you can make use of the Zend_Service_WindowsAzure_Storage_DynamicTableEntity class. This class inherits Zend_Service_WindowsAzure_Storage_TableEntity like an enforced schema class does, but contains additional logic to make it dynamic and not bound to a schema. Base properties provided by Zend_Service_WindowsAzure_Storage_DynamicTableEntity are:
Other properties can be added on the fly. Their Windows Azure table storage type will be determined on-the-fly: Example #18 Dynamicaly adding properties Zend_Service_WindowsAzure_Storage_DynamicTableEntity
Optionally, a property type can be enforced: Example #19 Forcing property types on Zend_Service_WindowsAzure_Storage_DynamicTableEntity
The Zend_Service_WindowsAzure_Storage_Table class automatically works with Zend_Service_WindowsAzure_Storage_TableEntity if no specific class is passed into Table Storage methods. Entities API examplesInserting an entityUsing the following code, an entity can be inserted into a table named "testtable". Note that the table has already been created before. Example #20 Inserting an entity
Retrieving an entity by partition key and row keyUsing the following code, an entity can be retrieved by partition key and row key. Note that the table and entity have already been created before. Example #21 Retrieving an entity by partition key and row key
Updating an entityUsing the following code, an entity can be updated. Note that the table and entity have already been created before. Example #22 Updating an entity
If you want to make sure the entity has not been updated before, you can make sure the Etag of the entity is checked. If the entity already has had an update, the update will fail to make sure you do not overwrite any newer data. Example #23 Updating an entity (with Etag check)
Deleting an entityUsing the following code, an entity can be deleted. Note that the table and entity have already been created before. Example #24 Deleting an entity
Performing queriesQueries in Zend_Service_WindowsAzure_Storage_Table table storage can be performed in two ways:
Using the following code, a table can be queried using a filter condition. Note that the table and entities have already been created before. Example #25 Performing queries using a filter condition
Using the following code, a table can be queried using a fluent interface. Note that the table and entities have already been created before. Example #26 Performing queries using a fluent interface
Batch operationsThis topic demonstrates how to use the table entity group transaction features provided by Windows Azure table storage. Windows Azure table storage supports batch transactions on entities that are in the same table and belong to the same partition group. A transaction can include at most 100 entities. The following example uses a batch operation (transaction) to insert a set of entities into the "testtable" table. Note that the table has already been created before. Example #27 Executing a batch operation
Table storage session handlerWhen running a PHP application on the Windows Azure platform in a load-balanced mode (running 2 Web Role instances or more), it is important that PHP session data can be shared between multiple Web Role instances. The Windows Azure SDK for PHP provides the Zend_Service_WindowsAzure_SessionHandler class, which uses Windows Azure Table Storage as a session handler for PHP applications. To use the Zend_Service_WindowsAzure_SessionHandler session handler, it should be registered as the default session handler for your PHP application: Example #28 Registering table storage session handler
The above classname registers the Zend_Service_WindowsAzure_SessionHandler session handler and will store sessions in a table called "sessionstable". After registration of the Zend_Service_WindowsAzure_SessionHandler session handler, sessions can be started and used in the same way as a normal PHP session: Example #29 Using table storage session handler
Warning
The Zend_Service_WindowsAzure_SessionHandler session handler should be registered before a call to session_start() is made! Zend_Service_WindowsAzure_Storage_QueueThe Queue service stores messages that may be read by any client who has access to the storage account. A queue can contain an unlimited number of messages, each of which can be up to 8 KB in size. Messages are generally added to the end of the queue and retrieved from the front of the queue, although first in/first out (FIFO) behavior is not guaranteed. If you need to store messages larger than 8 KB, you can store message data as a queue or in a table and then store a reference to the data as a message in a queue. Queue Storage is offered by Windows Azure as a REST API which is wrapped by the Zend_Service_WindowsAzure_Storage_Queue class in order to provide a native PHP interface to the storage account. API ExamplesThis topic lists some examples of using the Zend_Service_WindowsAzure_Storage_Queue class. Other features are available in the download package, as well as a detailed API documentation of those features. Creating a queueUsing the following code, a queue can be created on development storage. Example #30 Creating a queue
Deleting a queueUsing the following code, a queue can be removed from development storage. Example #31 Deleting a queue
Adding a message to a queueUsing the following code, a message can be added to a queue on development storage. Note that the queue has already been created before. Example #32 Adding a message to a queue
Reading a message from a queueUsing the following code, a message can be read from a queue on development storage. Note that the queue and message have already been created before. Example #33 Reading a message from a queue
The messages that are read using getMessages() will be invisible in the queue for 30 seconds, after which the messages will re-appear in the queue. To mark a message as processed and remove it from the queue, use the deleteMessage() method. Example #34 Marking a message as processed
Check if there are messages in a queueUsing the following code, a queue can be checked for new messages. Note that the queue and message have already been created before. Example #35 Check if there are messages in a queue
Note that messages that are read using peekMessages() will not become invisible in the queue, nor can they be marked as processed using the deleteMessage() method. To do this, use getMessages() instead.
|
|