googleapis / nodejs-storage

Node.js client for Google Cloud Storage: unified object storage for developers and enterprises, from live data serving to data analytics/ML to data archiving.
https://cloud.google.com/storage/
Apache License 2.0
896 stars 370 forks source link

How to list the subdirectories/subfolders in a folder #2423

Closed convers39 closed 6 months ago

convers39 commented 7 months ago

Thanks for stopping by to ask us a question! Please make sure to include:

For example, I have a folder structure like below, the root folder contains subfolders and files.

folder
 - sub1
   - file1.ext
 - sub2
   - nest1
     - file2.ext
 - file3.ext

I want to list all directories including or excluding nested ones.

folder/sub1/
folder/sub2/

# or with nested folders
folder/sub1/
folder/sub2/
folder/sub2/nest1/

I have tried with the code below:

      const [folders] = await client.getFiles({
        prefix: 'active/',
        delimiter: '/',
        autoPaginate: false,
      });
      console.log(
        'folders',
        folders.map((f) => f.name),
      );

which gives me

folder/
folder/file3.ext

↓gives me an empty array

      const [folders] = await client.getFiles({
        prefix: 'active',
        delimiter: '/',
        autoPaginate: false,
      });
      console.log(
        'folders',
        folders.map((f) => f.name),
      );

No

PLEASE READ: If you have a support contract with Google, please create an issue in the support console instead of filing on GitHub. This will ensure a timely response.

ddelgrosso1 commented 7 months ago

Hi @convers39 I think using matchGlob here is the better choice. For example using your structure to retrieve everything you can use bucket.getFiles({matchGlob: '**');

See the documentation here and here.

ddelgrosso1 commented 6 months ago

@convers39 were you able to implement a working solution?

ddelgrosso1 commented 6 months ago

Going to close this out as I think the above docs should help resolve the issue. If there are other questions or this doesn't fully answer the question, please feel free to reopen.