I want to keep track of the size of the uploaded files from users. Every user has his own folder (named after the uid) to upload the files with subfolders for projects. For my understanding it's not easy to get the total filesize for a folder with subfolders in firebase storage. So I found the Cloud Storage triggers (https://firebase.google.com/docs/functions/gcp-storage-events?hl=en) and thought if this would be a great solution to keep the total filesize up to date. I would update the userdata in firestore with the used storage size when a document is added/edited/deleted from a user. Is this really a good way?
The examples just show code for image manipulation and download, I couldn't find simple code for jsut getting the metadata filesize and then update a userrecord. Can someone help me here?
Big Thanks!
CodePudding user response:
Managed it so far with:
exports.storageSizeNewFile = functions.storage.object().onFinalize(async (object) => {
if (object.name == null) object.name = ""
var user = { uid: object.name.split("/")[0] }
var userData: any
admin.firestore().collection('users').doc(user.uid).get().then(doc => {
userData = doc.data()
if (userData.storage == null) userData.storage = { count: 0 }
userData.storage.count = userData.storage.count parseInt(object.size)
admin.firestore().collection('users').doc(user.uid).set({
storage: userData.storage
}, { merge: true });
})