使用 Node.js 从 Google Cloud Platform 存储桶获取文件夹列表

Gwe*_*en7 5 google-app-engine node.js google-cloud-storage google-cloud-platform

我想在 Node.js 应用程序上显示 Google Cloud Storage 存储桶中的所有文件夹,但只有 getFiles() 函数。

例如,名为/abc的文件夹,其中有 2 个文件/a/b。我只想得到/abc/而没有/abc/a/abc/b

应用程序.js

router.get('/', async(req, res) => {
  let bucketName = 'my-bucket'

  const storage = Storage();

  const [files] = await storage.bucket(bucketName).getFiles();
  let app = [];
  for (const file of files) {
    const [metadata] = await file.getMetadata();
    app.push(metadata);
  };
  res.render('views/list.ejs', {
    apps : app, 
  });
  });
Run Code Online (Sandbox Code Playgroud)

raz*_*zki 6

值得注意的是,像AWS上的S3。GCS 没有“目录”,只有路径。

https://cloud.google.com/storage/docs/gsutil/addlhelp/HowSubdirectoriesWork

.name您可以从 GCS 返回的对象上访问文件名。您还可以在呼叫中添加前缀.getFiles()

router.get('/', async (req, res) => {
    const bucketName = 'my-bucket'

    const storage = Storage()

    const [files] = await storage.bucket(bucketName).getFiles({ prefix: '/abc'})
    const objectNames = files.map(file => file.name)

    res.render('views/list.ejs', {
        apps: objectNames,
    })
})

Run Code Online (Sandbox Code Playgroud)