I created this piece of code as a rule too limit number of items stored there, but I still can add as many new images, as I want.
match /images/logo/{userId}/{imageId} {
allow write: if request.resource.size < 1 * 1024 * 1024
&& request.resource.contentType.matches('image/.*')
&& list(/images/logo/{userId}).size() < 1;
}
2
Answers
It’s not possible to limit the number of objects in a Cloud Storage bucket using Firebase security rules. There is no function
list()
available in security rules.You’re probably better off recording the count of objects in a database and use that in conjunction with a Cloud Functions trigger to determine if an object should be allowed to remain after uploading.
Let the client upload the logo image with the same filename, then there will be only one, even without rules.