2
votes

I am successfully uploading images using AngularFire2 to Firebase Storage.

I have the following upload code.

this.AfStorage.ref(`images/${userId}/${timeStamp}`).putString(base64Image,'data_url');

There are a few issues that I want to solve.

  1. How can I set a limit on the file size? Meaning I want the user to be able to upload files which are less than 10mb.
  2. How can I limit the file number? Meaning I want one user to be able to upload only 3 files.

If there are no firebase server size solutions do suggest some client size solutions.

Thanks

1

1 Answers

2
votes

To limit the size of uploads, see this example from the documentation:

service firebase.storage {
 match /b/{bucket}/o {
   match /images {
     // Cascade read to any image type at any path
     match /{allImages=**} {
       allow read;
     }

     // Allow write files to the path "images/*", subject to the constraints:
     // 1) File is less than 5MB
     // 2) Content type is an image
     // 3) Uploaded content type matches existing content type
     // 4) File name (stored in imageId wildcard variable) is less than 32 characters
     match /{imageId} {
       allow write: if request.resource.size < 5 * 1024 * 1024
                    && request.resource.contentType.matches('image/.*')
                    && request.resource.contentType == resource.contentType
                    && imageId.size() < 32
     }
   }
 }
}

There is no way to limit the number of files with security rules, so you'll have to look at workarounds such as shown here: