Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 如何仅列出GCS中的文件而不是文件夹?_Node.js_Google Cloud Storage - Fatal编程技术网

Node.js 如何仅列出GCS中的文件而不是文件夹?

Node.js 如何仅列出GCS中的文件而不是文件夹?,node.js,google-cloud-storage,Node.js,Google Cloud Storage,我是GCS的新手。我试图列出“文件夹”中的文件并下载它们,但是我的列表将文件夹名称作为索引0返回 如何只列出文件名而不列出目录名?或者我应该在得到列表后将数组移位()吗 const prefix = 'testFiles/'; //contains jpgs const delimiter = '/'; const options = { prefix: prefix, }; if (delimiter) { options.de

我是GCS的新手。我试图列出“文件夹”中的文件并下载它们,但是我的列表将文件夹名称作为索引0返回

如何只列出文件名而不列出目录名?或者我应该在得到列表后将数组移位()吗

const prefix = 'testFiles/'; //contains jpgs
const delimiter = '/';
    const options = {
        prefix: prefix,
      };

      if (delimiter) {
        options.delimiter = delimiter;
      }

const [files] = await storage.bucket(bucketName).getFiles();

files.forEach((file, i) => {
    options.destination = `./downloads/${i}.jpg`
    console.log(options);
    file.download(options)
});
}

谷歌云存储没有真正的“文件夹”。相反,使用方法的前缀和分隔符参数模拟它们

你这样做是正确的。但是,当使用前缀列出时,将返回以该前缀开头的所有对象,直到指定的增量。这意味着,正如您所发现的,如果有一个“目录占位符”对象(以
/
结尾的对象),它将被返回。注意,它还意味着该前缀的任何“子目录”也将被返回。例如,如果bucket中有以下对象列表:

testFiles/
testFiles/a
testFiles/b
testFiles/c/d
testFiles/v
如果使用
前缀=testFiles/
分隔符=/
调用列表对象,将返回以下内容:

items =
  testFiles/
  testFiles/a
  testFiles/b
  testFiles/v
prefixes =
  testFiles/c/

这是因为
testFiles/
实际上是GCS中存储桶中的一个对象。

那么我如何才能只获取:testFiles/a、testFiles/b、testFiles/v你不能,API不是这样工作的。在您的例子中,有一个名为
testFiles/
的对象。