I would like to allow users to only upload documents to their own bucket in storage with a maximum file size of 1MB, and still let them delete the files. I have added the following, which:
match /myusers/{userId}/{allPaths=**} {
allow write: if request.auth.uid == userId && request.resource.size < 1 * 1024 * 1024;
allow read: if request.auth.uid == userId;
}
I am testing both in the simulator and live in my project. This doesn't let me delete the document (access denied
). If I remove && request.resource.size < 1 * 1024 * 1024;
from the rule above, the the document can be deleted (but then won't prevent upload of files greater than 1MB.
I thought maybe it was rejecting it because request.resource
is null
, so I tried the following:
match /myusers/{userId}/{allPaths=**} {
allow write: if request.auth.uid == userId && (request.resource.size < 1 * 1024 * 1024 || request.resource == null);
allow read: if request.auth.uid == userId;
}
Still, deleting fails with the following error (in the simulator):
Error: simulator.rules line [5], column [16]. Property resource is undefined on object.
I have looked at all of these solutions and modified the rule as many ways as I can think of, to no avail:
Does anyone know how to set a max size for the file allowed but still allow for deleting?
null
. Can you try with onlyrequest.resource == null
? If that works correctly, try swapping the conditions in the or:(request.resource == null || request.resource.size < 1 * 1024 * 1024)
. – Frank van Puffelenallow write: if request.auth.uid == userId && request.resource == null
. And, in my screenshot, I did actually swap the order to(request.resource == null || request.resource.size < 1 * 1024 * 1024)
in case it was actually crashing onrequest.resource.size
due to attempting to accesssize
from anull
object (request.resource
). Both didn't work, but the solution I just posted below does! :) – Rbar