0
votes

My Question is similar to this one how to get a list of files in a google cloud storage folder using node js.

Only I am trying to just get the top level objects. I am trying to follow along here nodejs storage samples files.js, and when I try different combinations of "prefix" and "deliminator" I am having a hard time just getting the top level objects. I have tried a prefix of "" with a deliminator of "/". I have tried a prefix of "/" with a deliminator of "/". I tried a prefix of "/" with no deliminator. Nothing seems to work to allow me to get just a list of the top level objects. I seem to always get all files in the entire bucket. Any ideas on what the correct parameters for prefix and deliminator that might work? Are there other options that I am not aware of that could help?

To be a little more specific, given the following blobs:

  • folder1/file1.txt
  • folder2/file2.txt

I just want

  • folder1/
  • folder2/

A couple of things I have tried:

prefix: "", deliminator: "", results:

  • folder1/
  • folder1/file1.txt
  • folder2/
  • folder2/file2.txt

prefix: "", deliminator: "/", results:

  • No results...

Prefix: "/", deliminator: "", results:

  • No results...

Prefix: "/", deliminator: "/", results:

  • No results...

Prefix: "folder1/", deliminator: "", results:

  • folder1/
  • folder1/file1.txt

... as I would expect; however again I cannot figure out the right combination of prefix and deliminator to just get the two top object results that I want:

  • folder1/
  • folder2/
1
In your examples and when you say "nothing seems to work" ... what is the output received for each example? A read of the docs seems to say that a prefix of "" and a delimiter of "/" might work ... what did you find?Kolban
thank you for your reply. I updated my OP with those additional details.Sean W.

1 Answers

1
votes

I think that I finally found the answer. The answer is in a different site for their documentation. Specifically this site, and this page (for Bucket.getFiles()). Their example shows a call back that has (err, files, nextQuery, apiResponse) arguments. Which is strange because even this site doesn't mention nextQuery, and apiResponse as parameters for GetFilesCallback.

I do want to use async / await, and try / catch in my application so taking the code examples on the getFiles page, I was able to infer that I could get what I needed by doing this:

const [files, nextQuery, apiResponse] = await storage.bucket(bucketName).getFiles({autoPaginate: false, delimiter: "/", prefix: ""});

and then personally I found that the data within "apiResponse" to be much more useful than in files. What I was looking for was in apiResponse.items, and apiResponse.prefixes.

I am just pretty surprised that none of the documentation shows what that apiResponse is supposed to look like, nor does the GetFilesCallback page even show it as a possible parameter. In any case I was able to find what I was looking for. I think for my use case I will be using that apiResponse callback parameter more than the files parameter.