代码之家  ›  专栏  ›  技术社区  ›  SharpBCD

AppEngine上的NoDEJS,谷歌云/存储:如何检查文件夹是否存在,如果是,从中获取所有文件吗?

  •  0
  • SharpBCD  · 技术社区  · 6 年前

    我很难从Node.js转换到Appengine@google cloud/storage。 要验证文件夹是否是最新的,请执行一些简单的步骤:

    1. 检查文件夹是否存在。如果不是,则返回false,如果是->
    2. 检查文件是否有正确的名称

    在常规的javascript中,这样做不会有问题:

    if (!fs.existsSync(full_path)) {
        return false;
    }
    else {
        // else verify if all requiered files exist
        let check = true;
        let files = fs.readdirSync(full_path);
        allExpectedFiles.forEach((f) => {
            if (pfs.pfs.getFileName('pictures') !== f && files.indexOf(f) === -1) {
                // console.log(c.cyan, 'Not found file: ' + f);
                check = false;
            }
        });
        return check;
    }
    

    使用@google云/存储库。。。我就是做不到。

    我试过:

    // if folder does not exist:
    const dir = bucket.file(full_path);
    let doesExist = await dir.exists();
    console.info('For:', name, ' does folder exist?:',full_path, doesExist[0]);
    
    if (!doesExist[0]) {
        // folder does not exist so it's not uptodate
        return false;
    }
    

    即使是存在的人,我也犯了错误。

    注1:如果我检查一个文件(添加文件名而不是以“/”结尾),它就可以工作。但我需要检查一下文件夹。

    另外,我试图从文件夹中获取文件(通过检查文件强制文件夹为true),但也无法获取它们。

            let files = await bucket.getFiles({prefix: full_path, delimiter:'.json'});
        console.log(c.magenta,'Here are the files:');
        console.log(files);
    

    我什么都没有。我玩了前缀和分隔符,但我就是不能让他们工作。

    注2:我确实读过: How subdirectories works ,但是。。。很明显我搞不懂。

    有什么建议吗?我知道这应该是一个很简单的任务,我就是做不到。

    以后编辑: 我设法得到了这些文件,但只是“all/the/long/path/filename.json”。我可以接受它,我将提取文件名,但似乎有问题,这不可能是那么困难。这是我的代码:

    let files = await bucket.getFiles({
            prefix   : full_path
        });
        files = files[0];
        files.forEach((f)=>{
            console.log(c.magenta, 'Fisierele cele multe:', f.name);
        });
    
    1 回复  |  直到 6 年前
        1
  •  2
  •   Dan Cornilescu    6 年前

    云存储中确实没有目录。它们只是基于文件路径进行模拟。

    因此,跳过这些步骤,检查目标目录是否存在并创建它,然后直接检查“目录”中的文件,根据要检查的源目录构建它们的文件路径。