【问题标题】:How to get a list of files in a Google Cloud Storage folder using Node.js?如何使用 Node.js 获取 Google Cloud Storage 文件夹中的文件列表?
【发布时间】:2018-09-16 00:26:52
【问题描述】:
【问题讨论】:
标签:
node.js
google-cloud-storage
【解决方案3】:
如果您的存储桶中有大量文件,您可能需要考虑将它们列为流,以便在查询期间数据不会保留在内存中。
GetFiles,一口气列出所有内容:
admin.storage().bucket()
.getFiles({ prefix: 'your-folder-name/', autoPaginate: false })
.then((files) => {
console.log(files);
});
getFilesStream,将所有内容都列为流:
admin.storage().bucket()
.getFilesStream({ prefix: 'your-folder-name/' })
.on('error', console.error)
.on('data', function (file) {
console.log("received 'data' event");
console.log(file.name);
})
.on('end', function () {
console.log("received 'end' event");
});
完整文档和示例:link