Limit number of files in a firebase storage path

六眼飞鱼酱① 提交于 2019-12-30 10:44:05

问题


I want to limit the number of files that a user can upload to a firebase storage path. Here is my firebase storage rule:

   service firebase.storage {
  match /b/jobsdbn.appspot.com/o {
    match /images/Business/{userId}/{imageId} {
       allow read
       allow write: if request.auth.uid == userId && (request.resource == null || ( (list(/images/Business/$(username)).size() <= 5) && imageId.size() < 50 && request.resource.size < 10 * 1024 * 1024 && request.resource.contentType.matches('image/.*')))

    }
  }
}

I have added list(/images/Business/$(username)).size() <= 5 from this article: https://groups.google.com/forum/#!topic/firebase-talk/ZtJPcEJr0Mc

but I always get 403 and not sure if "username" is global variable or not but I have used different variables and still cannot make it work.


回答1:


In case any one needs an answer I found the solution by using firebase cloud functions:

exports.getNumOfFile = functions.storage.object().onChange(event => {
    const object = event.data; // The Storage object.
    const fileBucket = object.bucket; // The Storage bucket that contains the file.
    const bucket = gcs.bucket(fileBucket);
    bucket.getFiles({
        prefix: "[path]", //Path like what you use to get reference of your files in storage 
    }, (error, files) => {
        console.log(files.length);
    });
});

You can find more options here:

https://googlecloudplatform.github.io/google-cloud-node/#/docs/storage/1.1.0/storage/bucket?method=getFiles



来源:https://stackoverflow.com/questions/44092953/limit-number-of-files-in-a-firebase-storage-path

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!