I'm trying to write a simple function that will iterate over all the files I have in a folder in Firebase storage using a Firebase Cloud Function. I have spent hours trying every example online and reading the documentation.
This is what I currently have:
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);
})
});
I'm getting an error that storage.ref() is not a function.
If I try:
storage.listAll()
It also tells me that listAll is not a function.
I really didn't think it would be this hard to just get the files in a folder.
What am I doing wrong?
UPDATED CODE THAT NOW WORKS
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)
})
});
});
return admin.storage().bucket().getFiles({ prefix: "postImages/" }).then(function(data) { const files = data[0]; ... return null; });