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 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.
This 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.
Using the following code, a blob storage container can be created on development storage.
Creating a storage container
1 2 3 4 | $storageClient = new Zend_Service_WindowsAzure_Storage_Blob();
$result = $storageClient->createContainer('testcontainer');
echo 'Container name is: ' . $result->Name;
|
Using the following code, a blob storage container can be removed from development storage.
Deleting a storage container
1 2 | $storageClient = new Zend_Service_WindowsAzure_Storage_Blob();
$storageClient->deleteContainer('testcontainer');
|
Using 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.
Storing a blob
1 2 3 4 5 6 7 8 | $storageClient = new Zend_Service_WindowsAzure_Storage_Blob();
// upload /home/maarten/example.txt to Azure
$result = $storageClient->putBlob(
'testcontainer', 'example.txt', '/home/maarten/example.txt'
);
echo 'Blob name is: ' . $result->Name;
|
Using 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.
Copying a blob
1 2 3 4 5 6 7 8 | $storageClient = new Zend_Service_WindowsAzure_Storage_Blob();
// copy example.txt to example2.txt
$result = $storageClient->copyBlob(
'testcontainer', 'example.txt', 'testcontainer', 'example2.txt'
);
echo 'Copied blob name is: ' . $result->Name;
|
Using 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.
Downloading a blob
1 2 3 4 5 6 | $storageClient = new Zend_Service_WindowsAzure_Storage_Blob();
// download file to /home/maarten/example.txt
$storageClient->getBlob(
'testcontainer', 'example.txt', '/home/maarten/example.txt'
);
|
By 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.
Making a blob publicly available
1 2 3 4 5 6 7 | $storageClient = new Zend_Service_WindowsAzure_Storage_Blob();
// make container publicly available
$storageClient->setContainerAcl(
'testcontainer',
Zend_Service_WindowsAzure_Storage_Blob::ACL_PUBLIC
);
|
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. http://myaccount.blob.core.windows.net/somefile.txt.
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.
The 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 usage of blob storage stream wrapper
1 2 3 4 5 | $fileHandle = fopen('azure://mycontainer/myfile.txt', 'r');
// ...
fclose($fileHandle);
|
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:
Registering the blob storage stream wrapper
1 2 3 4 5 6 7 8 9 | $storageClient = new Zend_Service_WindowsAzure_Storage_Blob();
// registers azure:// on this storage client
$storageClient->registerStreamWrapper();
// or:
// regiters blob:// on this storage client
$storageClient->registerStreamWrapper('blob://');
|
To unregister the stream wrapper, the unregisterStreamWrapper() method can be used.
The source code of this file is hosted on GitHub. Everyone can update and fix errors in this document with few clicks - no downloads needed.