After updating my code to use the latest version of Azure's PHP SDK, lots of things changed, including using namespaces but also including the way certain things are done. There used to be a function called blobExists() which was any easy way to, you guessed, check whether a blob exists in a given container.

For some reason, this is not present any more, perhaps because it was previously implemented in the SDK (not the API itself) and hasn't been duplicated but either way, I wanted one and I have written my own version!

Note that I am using it in the Yii framework so there is a yii error log call that you won't need but otherwise it should work. I have also included the namespaces in the code for completeness, although you can instead add a "use" statement to the top of your class.

/**
* Helper function to find out if the given blob exists on Azure storage
* @param string $container The container name to look in
* @param string $blob The blob name to look for
* @return boolean True if the blob exists
* @throws \yii\web\ServerErrorHttpException
*/
public static function BlobExists($container, $blob)
{
try
{
$proxy = \WindowsAzure\Common\ServicesBuilder::getInstance()->createBlobService("DefaultEndpointsProtocol=https;AccountName=accountname;AccountKey=yourkey");
$options = new \WindowsAzure\Blob\Models\ListBlobsOptions();
$options->setPrefix(basename($blob)); // Use prefix to limit the number of results
$result = $proxy->listBlobs($container, $options);
$blob_array = $result->getBlobs();
return count($blob_array) == 1;
}
catch(\WindowsAzure\Common\ServiceException $e)
{
Yii::error($e->getMessage());
throw new \yii\web\ServerErrorHttpException();
}
}

Note that basename() is used since setPrefix() doesn't work with a full filename, only the first part. This will cause problems for files that have the same name but different extensions, in which case, you would need to do some extra checking with the results to do a full match.

There are also other preferred ways to pass the connection data to the ServicesBuilder using environment variables etc. see https://github.com/Azure/azure-sdk-for-php for details.