cloudfiles API:当容器不存在时,get_container 未捕获异常

发布于 2024-12-17 12:04:10 字数 425 浏览 6 评论 0原文

我使用rackspace cloudfiles API 动态上传文件,它需要做的第一件事是检查容器是否存在,如果不存在,则创建它。

所以我写了以下内容:

if($container = $conn->get_container('my_container')){
   echo 'yay';

} else {
   $container = $conn->create_container('my_container');
   $container->make_public();   
}                                           

但是如果容器不存在 get_container 会抛出异常,因此我会收到致命错误。完成我在这里尝试做的事情的最佳方法是什么?

I'm using the rackspace cloudfiles API to upload files on the fly and the first thing it needs to do is check whether the container exists and if not, create it.

So I write the following:

if($container = $conn->get_container('my_container')){
   echo 'yay';

} else {
   $container = $conn->create_container('my_container');
   $container->make_public();   
}                                           

But if the container doesn't exist get_container throws an exception and so I get a fatal error. What's the best way to do what I'm trying to do here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小忆控 2024-12-24 12:04:10
try {
    $container = $conn->get_container('my_container');
    $obj_list = $container->list_objects();
    print_r($obj_list);
}
catch (Exception $e) {
    $container = $conn->create_container('my_container');
    //$container->make_public();
}
try {
    $container = $conn->get_container('my_container');
    $obj_list = $container->list_objects();
    print_r($obj_list);
}
catch (Exception $e) {
    $container = $conn->create_container('my_container');
    //$container->make_public();
}
夜还是长夜 2024-12-24 12:04:10

您也可能会遇到显式异常:

try {
    $container = $conn->get_container('my_container');
    $obj_list = $container->list_objects();
    print_r($obj_list);
}
catch (NoSuchContainerException $e) {
    $container = $conn->create_container('my_container');
}

如果您遇到网络问题或相关问题,这不会失败。

you could expect an explicit exception also:

try {
    $container = $conn->get_container('my_container');
    $obj_list = $container->list_objects();
    print_r($obj_list);
}
catch (NoSuchContainerException $e) {
    $container = $conn->create_container('my_container');
}

this will not fail if you have networking problems or something related.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文