【问题标题】:Why can't I catch the ServiceException on the Azure PHP sdk?为什么我无法在 Azure PHP sdk 上捕获 ServiceException?
【发布时间】:2018-03-14 18:52:32
【问题描述】:

我试图找出一个 blob 是否存在。当 blob 不存在时,我的 try-catch 和 Azure 的 ServiceException 根本不会被捕获。我尝试按照here 的步骤进行操作。

public function checkBlobExists($path) {

    $container = config('azure.storage.container');
    $blobClient = ServicesBuilder::getInstance()->createBlobService(config('azure.storage.connection_string'));

    try {

        $blob = $blobClient->getBlob($container, $path); 
        return true;

    } catch (ServiceException $e) {
        return false;
    }

    return false;
}

这是一些错误堆栈:

ServiceException in ServiceRestProxy.php line 491:
Fail:
Code: 404
Value: The specified blob does not exist.
details (if any): .
in ServiceRestProxy.php line 491
at ServiceRestProxy::throwIfError(object(Response), array('200', '206')) in ServiceRestProxy.php line 409
at ServiceRestProxy->MicrosoftAzure\Storage\Common\Internal\{closure}(object(ClientException)) in Promise.php line 203

【问题讨论】:

  • 离题评论 .... 恕我直言getBlob 在下载 blob 时检查 blob 是否存在可能是一项非常昂贵的操作。为什么不用getBlobProperties这个轻量级的操作呢?
  • @GauravMantri 我最终使用了getBlobProperties。好点子。

标签: php azure blob


【解决方案1】:

您可能没有使用完全限定的异常类名。试试:

//...
} catch (\MicrosoftAzure\Storage\Common\Exceptions\ServiceException $e) {
//...
}

【讨论】:

  • 成功了。不过我很好奇,因为我use 我的ServicesBuilder 完全相同。这里有什么问题?编辑:我错过了Exceptions 目录。
  • 就好像 ServiceException 与您当前所在的文件夹不同,并且解释器找不到它。你仍然可以让catch (ServiceException $e) 工作,但你必须像这样声明一个 use 语句:use MicrosoftAzure\Storage\Common\Exceptions\ServiceException; 你可能在文件顶部有一个 ServiceBuilder 的 use 语句 (use MicrosoftAzure\Storage\Common\ServicesBuilder;)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-27
  • 1970-01-01
  • 2018-03-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多