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

Recursively deleting a folder in Google Cloud Storage

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

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

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

PHP Warning: Cloud Storage Error: NOT FOUND in /base/data/home/runtimes/php/sdk/google/appengine/ext/cloud_storage_streams/CloudStorageDirectoryClient.php on line 223

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");

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

gsutil provides the illusion of a hierarchical file tree atop the “flat” name space supported by the Google Cloud Storage service. To the service, the object gs://your-bucket/abc/def/ghi.txt is just an object that happens to have “/” characters in its name. There are no “abc” or “abc/def” directories; just a single object with the given name.

所以你实际上不需要 rmdir($dirPath); 语句(我怀疑那是导致警告的原因)。