Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Limit number of files in a firebase storage path

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.

like image 748
user1548745 Avatar asked Oct 29 '25 01:10

user1548745


1 Answers

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

like image 70
user1548745 Avatar answered Oct 31 '25 11:10

user1548745



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!