仅获取存储桶的顶级对象 [英] Get Only The Top Level Objects of A Bucket
问题描述
我的问题与此类似 如何使用node js获取谷歌云存储文件夹中的文件列表.
只有我想获得顶级对象.我正在尝试跟随这里 nodejs 存储示例文件.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?
更具体一点,给定以下 blob:
To be a little more specific, given the following blobs:
- 文件夹1/file1.txt
- 文件夹2/file2.txt
我只是想要
- 文件夹 1/
- 文件夹2/
我尝试过的几件事:
前缀:",分隔符:",结果:
prefix: "", deliminator: "", results:
- 文件夹 1/
- 文件夹1/file1.txt
- 文件夹2/
- 文件夹2/file2.txt
前缀:",分隔符:/",结果:
prefix: "", deliminator: "/", results:
- 没有结果...
前缀:/",分隔符:",结果:
Prefix: "/", deliminator: "", results:
- 没有结果...
前缀:/",分隔符:/",结果:
Prefix: "/", deliminator: "/", results:
- 没有结果...
前缀:folder1/",分隔符:",结果:
Prefix: "folder1/", deliminator: "", results:
- 文件夹 1/
- 文件夹1/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:
- 文件夹 1/
- 文件夹2/
推荐答案
我想我终于找到了答案.答案在他们的文档的不同站点中.特别是 本网站 和 这个页面(用于 Bucket.getFiles()).他们的示例显示了具有 (err, files, nextQuery, apiResponse) 参数的回调.这很奇怪,因为即使这个网站也没有提到 nextQuery 和 apiResponse 作为 的参数GetFilesCallback.
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.
我确实想在我的应用程序中使用 async/await,并尝试/catch,因此请使用 getFiles 页面,我能够推断出我可以通过这样做得到我需要的东西:
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: ""});
然后我个人发现apiResponse"中的数据比文件中的数据有用得多.我正在寻找的是 apiResponse.items
和 apiResponse.prefixes
.
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
.
令我惊讶的是,没有任何文档显示 apiResponse
应该是什么样子,GetFilesCallback 页面甚至也没有将其显示为可能的参数.无论如何,我都能找到我想要的东西.我认为对于我的用例,我将使用 apiResponse
回调参数而不是 files
参数.
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.
这篇关于仅获取存储桶的顶级对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!