Recursively deleting a folder in Google Cloud Stor

2019-07-17 16:04发布

I have the following code that should delete the directory and everything inside it.

It seems to work fine but for some reason I get the following warnings in my app engine logs when the code runs.

Does anyone know why this would happen or if there is a better way to avoid these errors?

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

1条回答
ら.Afraid
2楼-- · 2019-07-17 16:44

GCS doesn't actually have (sub)directories, they're "faked" by extracting them from the path-like segments of filenames:

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.

enter image description here

So you don't actually need the rmdir($dirPath); statement (I suspect that's the one causing the warning).

查看更多
登录 后发表回答