Reputation: 1268
I am trying to impose a limit on file upload size using the code below (request.resource.size < 5 * 1024 * 1024
) and when uploading an image I get a permission denied error.
After running it in the simulator, I concluded that the requirement causing errors was the size below 5MB check. In the simulator an error is thrown (Property size is undefined on object.
) and when I inspected the request.resource object, there was no size
property, only contentType
, name
and bucket
.
How can I correctly impose this 5MB limit?
rules_version = '2';
service firebase.storage {
match /b/{bucket}/o {
match /profile_images/{profileImgId}/{imgId} {
allow create, update: if request.auth.uid == profileImgId
&& request.resource.size < 5 * 1024 * 1024
&& request.resource.contentType.matches('(?i)image/(jpeg|jpg|png)');
allow delete: if false;
allow get: if request.auth.uid == profileImgId;
}
}
}
Upvotes: 3
Views: 1092
Reputation: 151
Just a suggestion I also used the same code as fatalcoder524 shown above for my android application. But i also wanted to catch the exception that whether it was due to size exception or really a failure due to some other reason so i took the help of Storage Exception errorcodes to identify them on client side. Here's a snippet of my code..
int errCode = ((StorageException) exception).getErrorCode();
if(errCode == ERROR_NOT_AUTHORIZED)
Toast.makeText(view.getContext(),"Size of file must be\nless than 5 MB", Toast.LENGTH_LONG).show();
Upvotes: 2
Reputation: 1480
This security rules in firebase storage works for me:-
5MB=5242880 bytes
service firebase.storage {
match /b/{bucket}/o {
match /profile {
allow read, write: if request.resource.size < 5 * 1024 * 1024;
}
}
}
Upvotes: 2