Microsoft Azure Open Source Development Support Team Blog

Support for Open Source Technologies on Microsoft Azure App Service

Azure SDK for PHP: Updating Blob Properties

Here is sample code to update Blob Properties using Azure SDK for PHP.

This code will traverse through all the blobs within the specified container and update the cache-control to “public, max-age=604800”.

define("__BLOBNAME__", "name");
define("__CONTAINERNAME__", "container");
define("__BLOBKEY__", "key");

use WindowsAzure\Common\ServicesBuilder;
use WindowsAzure\Common\ServiceException;
use WindowsAzure\Common\CloudConfigurationManager;
use WindowsAzure\Blob\Models\Block;
use WindowsAzure\Blob\Models\CreateContainerOptions;
use WindowsAzure\Blob\Models\ListContainersOptions;
use WindowsAzure\Blob\Models\CreateBlobOptions;
use WindowsAzure\Blob\Models\CommitBlobBlocksOptions;

use WindowsAzure\Blob\Models\BlobProperties;

$connectionString = "DefaultEndpointsProtocol=http;AccountName=" . __BLOBNAME__ . ";AccountKey=" . __BLOBKEY__ . "";

if (null == $connectionString || "" == $connectionString) {
    echo "Did not find a connection string whose name is 'StorageConnectionString'.";

// Create blob REST proxy.
$blobRestProxy = ServicesBuilder::getInstance()->createBlobService($connectionString);

try {
    // List blobs.
    $blob_list = $blobRestProxy->listBlobs(__CONTAINERNAME__);
    $blobs = $blob_list->getBlobs();
    // iterate over blobs
	foreach ($blobs as $blob) {
		$options = new WindowsAzure\Blob\Models\CreateBlobOptions();
		$options->setBlobCacheControl("public, max-age=604800");
		echo 'setting ... ';
		$blobRestProxy->setBlobProperties(__CONTAINERNAME__,$blob->getName(), $options);
} catch (ServiceException $e) {
    // Handle exception based on error codes and messages.
    // Error codes and messages are here:
    $code = $e->getCode();
    $error_message = $e->getMessage();
    echo $code . ": " . $error_message;