Firebase storage - Limit size of image that users upload to firebase storage

Per the Firebase Storage Security Rules docs, you can write rules that check the size of an uploaded file:

service firebase.storage {
  match /b/<bucket>/o {
    match /files/{fileName} {
      allow read;
      allow write: if request.resource.size < 10 * 1024 * 1024; // 10MB limit for instance
    }
  }
}

RESIZE ON CLIENT-SIDE USING JS

If you goal is not necessarily to limit the user, but to keep the images in a low size, you could better resize the image on client side using JavaScript.

That was the perfect solution for us, saving:

  1. Space in our hosting,
  2. Money for that hosting space,
  3. Time of the visitor during upload (usually more than 10x less),
  4. Money of the visitor when uploading through mobile limited bandwith,
  5. Problems and bad experiences to the visitor when trying to upload a big photo that we would limit with other solutions

If you are interested, you could follow this other answer from dcollien, in which we based the resizing:

https://stackoverflow.com/a/31669706/1920145

As one can see in that answer, the usage method is really simple, and the script to include is only one small JS / ES6 file.

I believe this guy (dcollien) deserves much more merit, so I suggest you to vote his answer and this answer up (so others can find it as relevant).