List top level folders in GCP GCS from Cloud Function bucket API?
I have a GCS bucket that has objects like...
myfile.pdf
myimg.png
folder001/stuff/<some files or deep folders>
folder002/<some files or deep folders>
.
.
.
someOtherFolderName00n/<some files or deep folders>
... and just want to get the list of top level folders folder001, ..., someOtherFolderName00n
.
I have a snippet of code in GCP's Cloud Functions using the Bucket API that looks like...
const admin = require('firebase-admin');
admin.initializeApp();
const sourceBucket = admin.storage().bucket("test_source_001");
exports.my_function = async (event, context) => {
// get top level bucket folders
const [sourceFiles] = await sourceBucket.getFiles({
prefix: '',
delimiter: '/'
});
// extract name property from each object
const sourceFileNames = sourceFiles.map((file) => file.name);
console.log(sourceFileNames)
... but this actually ends up listing everything in that bucket except for just top level directories (even the top level files that don't even have trailing '/'s), so I get a list like
myfile.pdf
myimg.png
folder001/stuff/
folder001/stuff/file1
...
folder001/stuff/fileN
folder002/file1
...
folder002/fileN
...
someOtherFolderName00n/file1
...
someOtherFolderName00n/fileN
I think I could just do something like...
s = new Set()
for (let f of sourceFileNames) {
s.add(f.split('/')[0])
}
... but is there any way to just have the getFiles
query return top level folders in the first place? (New to using GCP and Cloud Functions, so wonder if I'm just missing something simple here).