How to get a list of files in a Google Cloud Storage folder using Node.js?

后端 未结 2 1914
遇见更好的自我
遇见更好的自我 2020-12-09 19:21

Using bucket.getFiles() it is possible to get all the files in a bucket.

My bucket has thousands of files and I really only want to get metadata on the

相关标签:
2条回答
  • 2020-12-09 19:47

    Google cloud storage doesn't have folders/sub directories. It is an illusion on top of the flat namespace. i.e what you see as sub directories are actually objects which has a "/" character in its name.

    You can read more about how Google cloud storage subdirectories work at the following link https://cloud.google.com/storage/docs/gsutil/addlhelp/HowSubdirectoriesWork

    So by setting the prefix parameter of GetFilesRequest to the sub directory name you are interested in will return the object you are looking for.

    0 讨论(0)
  • 2020-12-09 19:54

    There is an ability to specify a prefix of the required path in options, e.g.

    async function readFiles () {
      const [files] = await bucket.getFiles({ prefix: 'users/user42'});
      console.log('Files:');
      files.forEach(file => {
        console.log(file.name);
      });
    };
    

    This example is the last here: https://cloud.google.com/nodejs/docs/reference/storage/2.3.x/Bucket#getFiles

    It's sad that there is so little information about possible options for methods. Hope documenting team will change it soon.

    0 讨论(0)
提交回复
热议问题