【发布时间】:2021-08-11 23:44:36
【问题描述】:
我正在尝试编写一个简单的函数,该函数将使用 Firebase 云函数遍历我在 Firebase 存储文件夹中的所有文件。我花了几个小时在线尝试每个示例并阅读文档。
这是我目前拥有的:
exports.removeUnsizedImages = functions.pubsub.schedule('every 2 minutes').onRun((context) => {
const storage = admin.storage();
var storageRef = storage.ref();
storageRef.listAll().then(function(result) {
console.log("*", result);
})
});
我收到 storage.ref() 不是函数的错误。
如果我尝试:
storage.listAll()
它还告诉我 listAll 不是一个函数。
我真的没想到将文件放在一个文件夹中会这么难。
我做错了什么?
现在可以使用的更新代码
exports.removeUnsizedImages = functions.pubsub.schedule('every 24 hours').onRun((context) => {
admin.storage().bucket().getFiles({ prefix: "postImages/" }).then(function(data) {
const files = data[0];
files.forEach(function(image) {
console.log("***** ", image.name)
})
});
});
【问题讨论】:
-
你应该返回异步工作返回的承诺,见firebase.google.com/docs/functions/terminate-functions。类似
return admin.storage().bucket().getFiles({ prefix: "postImages/" }).then(function(data) { const files = data[0]; ... return null; });
标签: node.js firebase google-cloud-functions firebase-storage