Search code examples
phpazureazure-storageazure-blob-storage

Check if blob exists in Azure


I'm wondering if there is a way to check if a blob exists in a container?

    $blob = $blobRestProxy->getBlob("mycontainer", "myblobname");
    if($blob){
        return 'exists';
    } else {
        return 'not exists';
    }

I've tried this but im getting this message whenever the blob does not exists:

BlobNotFoundThe specified blob does not exist.

If exists, the code returns 'exists' naturally. I'm not interested in listing all blobs in the container and iterating until I find a match cause I have a lot of blobs.


Solution

  • When the blob does not exist, the function getBlob will raise a ServiceException exception and exit the PHP progress, the following code will not work.

    Please try to add the try catch statement in your code, E.G.

    try {
        $blob = $tableRestProxy->getBlob("mycontainer", "myblobname");
        return 'exists';
    } catch (ServiceException $e) {
        return 'not exists';
    }