Check if blob exists in Azure

与世无争的帅哥 提交于 2019-12-12 00:07:23

问题


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.


回答1:


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';
} 



回答2:


so:

$exists = $storageClient->blobExists(<container name>, <blob name>);

Should give you what you're after.



来源:https://stackoverflow.com/questions/37297646/check-if-blob-exists-in-azure

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!