【问题标题】:Check if blob exists in Azure检查 Azure 中是否存在 blob
【发布时间】:2020-02-19 00:00:45
【问题描述】:

我想知道是否有办法检查容器中是否存在 blob?

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

我已经尝试过了,但只要 blob 不存在,我就会收到此消息:

BlobNotFound指定的 blob 不存在。

如果存在,代码自然返回“存在”。我对列出容器中的所有 blob 并迭代不感兴趣,直到找到匹配项,因为我有很多 blob。

【问题讨论】:

    标签: php azure azure-storage azure-blob-storage


    【解决方案1】:

    当blob不存在时,函数getBlob会引发ServiceException异常并退出PHP进程,下面的代码将不起作用。

    请尝试在您的代码中添加 try catch 语句,例如

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

    【讨论】:

    • 这个。我以为我有 try catch ServiceException,但问题是我没有正确导入 ServiceException,所以没有处理异常。使用 MicrosoftAzure\Storage\Common\ServiceException;
    【解决方案2】:

    所以:

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

    应该给你你所追求的。

    【讨论】:

    • storageClient 是什么类型? BlobRestProxy 没有 blobExists 方法。
    猜你喜欢
    • 2013-07-05
    • 2011-02-08
    • 1970-01-01
    • 1970-01-01
    • 2012-06-17
    • 2011-10-14
    • 1970-01-01
    • 2020-07-22
    • 2017-11-02
    相关资源
    最近更新 更多