【问题标题】:Create Path at Microsoft Azure with PHP使用 PHP 在 Microsoft Azure 上创建路径
【发布时间】:2018-12-04 04:33:07
【问题描述】:

我正在构建一个应用程序,我需要在其中使用 Azure 的 PHP SDK 动态创建一些目录。

我是用循环来做的,但我不确定这是否是正确的做法,所以这是我的代码;

我无法创建已经存在的路径,因此我必须逐级检查目录是否存在,然后输入并重复。

public function generateDirectory($path)
{
    $pathArray = explode("/", $path);

    $currentPath = "";
    try {
        foreach ($pathArray as $key => $slice) {
            $directories = $this->fileClient->listDirectoriesAndFiles("abraco", $currentPath)->getDirectories();
            $currentPath .= $slice . "/";
            $exists = false;
            foreach ($directories as $key => $directory) {
                if ($directory->getName() === $slice) {
                    $exists = true;
                    break;
                }
            }
            if (!$exists) {
                $this->fileClient->createDirectory("abraco", $currentPath);
            }

        }
        return true;
    } catch (Exception $e) {
        return false;
    }

}

它不应该有一种方法来创建带有子文件夹的完整路径吗?我认为这种方式不适合表演。

【问题讨论】:

    标签: php azure azure-storage


    【解决方案1】:

    它不应该有一种方法来创建带有子文件夹的完整路径吗?我认为这种方式不适合表演。

    我同意你的观点,有一种方法可以创建带有子文件夹的完整路径会更好。 但是目前,正如您提到的,如果我们要创建带有子文件夹的完整路径,我们需要逐级创建目录文件夹。

    如果您在通过 PHP SDK 创建多级目录结构时使用 fiddler 捕获请求,您可以使用以下 Rest API 找到它

    https://myaccount.file.core.windows.net/myshare/myparentdirectorypath/mydirectory?
    restype=directory
    

    更多信息请参考 Azure 文件存储Create directory API

    myparentdirectorypath 可选。要在其中创建 mydirectory 的父目录的路径。如果省略父目录路径,则在指定的共享中创建目录。

    如果指定,父目录必须已经存在在共享中,然后才能创建 mydirectory。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-07-08
      • 2012-07-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-09
      • 2018-10-19
      相关资源
      最近更新 更多