递归删除Google Cloud Storage中的文件夹 [英] Recursively deleting a folder in Google Cloud Storage

查看:132
本文介绍了递归删除Google Cloud Storage中的文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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警告:云存储错误:在第223行的/base/data/home/runtimes/php/sdk/google/appengine/ext/cloud_storage_streams/CloudStorageDirectoryClient.php中未找到

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, they're "faked" by extracting them from the path-like segments of filenames:

gsutil提供了层次结构文件树的错觉 Google云端存储服务支持的固定"名称空间.到 服务,对象gs://your-bucket/abc/def/ghi.txt只是一个 名称恰好带有"/"字符的对象.没有 "abc"或"abc/def"目录;与给定的单个对象 名称.

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);语句(我怀疑是引起警告的那个).

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

这篇关于递归删除Google Cloud Storage中的文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆