递归删除Google云端存储中的文件夹

时间:2016-02-26 15:36:19

标签: php google-app-engine google-cloud-storage

我有以下代码应该删除目录及其中的所有内容。

它似乎工作正常,但出于某种原因,我在代码运行时在我的应用引擎日志中收到以下警告

有谁知道为什么会发生这种情况,或者是否有更好的方法来避免这些错误?

  

PHP警告:云存储错误:未在第223行的/base/data/home/runtimes/php/sdk/google/appengine/ext/cloud_storage_streams/CloudStorageDirectoryClient.php中找到

function deleteDir($dirPath) 
{
    if (! is_dir($dirPath)) {
        die("not a directory");
    }
    if (substr($dirPath, strlen($dirPath) - 1, 1) != '/') {
        $dirPath .= '/';
    }
    $files = glob($dirPath . '*', GLOB_MARK);
    foreach ($files as $file) {
        if (is_dir($file)) {
            deleteDir($file);
        } else {
            unlink($file);
        }
    }
    rmdir($dirPath);
}

deleteDir("gs://folder/folder");

1 个答案:

答案 0 :(得分:4)

GCS doesn't actually have (sub)directories,他们"伪造"通过从类似路径的文件名段中提取它们:

  

gsutil提供了分层文件树的错觉   Google云端存储服务支持的“平面”名称空间。至   服务,对象gs://your-bucket/abc/def/ghi.txt只是一个   碰巧在其名称中包含“/”字符的对象。没有   “abc”或“abc / def”目录;只有一个给定的对象   名。

enter image description here

所以你实际上并不需要rmdir($dirPath);声明(我怀疑是导致警告的声明)。