简体   繁体   English

Firebase 使用云存储触发器上传的用户文件的存储轨道大小

[英]Firebase Storage track size of uploaded user files with Cloud Storage triggers

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.每个用户都有自己的文件夹(以 uid 命名)来上传带有项目子文件夹的文件。 For my understanding it's not easy to get the total filesize for a folder with subfolders in firebase storage.据我了解,获取 firebase 存储中包含子文件夹的文件夹的总文件大小并不容易。 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.因此,我找到了 Cloud Storage 触发器 ( https://firebase.google.com/docs/functions/gcp-storage-events?hl=en ),并认为这是否是使总文件大小保持最新的好解决方案。 I would update the userdata in firestore with the used storage size when a document is added/edited/deleted from a user.当从用户添加/编辑/删除文档时,我会使用使用的存储大小更新 firestore 中的用户数据。 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!十分感谢!

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 });

})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM