我很难从Node.js转换到Appengine@google cloud/storage。
要验证文件夹是否是最新的,请执行一些简单的步骤:
-
检查文件夹是否存在。如果不是,则返回false,如果是->
-
检查文件是否有正确的名称
在常规的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);
});