0

I want put a limitation on firebase storage. That is user can upload a pdf file of maximum size of 7MB. And How can i reduce the size of pdf file? is there any way in android.

I don't know the solution of my question. I find a lot but doesn't get my answer. Can someone help me with firebase security rules? I am not familiar with that doesn't find any answer about this.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807

2 Answers2

0

The Firebase documentation on security rules has an example that shows (amongst others) how to set the maximum size of files that can be stored:

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
     }
   }
 }
}

Since these rules will only be applied after the actual upload has completed (but before the file is actually stored in your storage bucket), you'll typically also want to check the file size in your Android app before uploading it, to prevent using bandwidth for a file that is too large. For some examples of how to check the local file size on Android, see Get the file size in android sdk?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • I want to put limitation on pdf type files not image type. How can i do that? – Muhammad Ali May 29 '19 at 17:07
  • 1
    Since the MIME type of a PDF is `application/pdf`, that would be `&& request.resource.contentType.matches('application/pdf')`. – Frank van Puffelen May 29 '19 at 17:22
  • I have a folder in firebase storage named "post files". Now how i can make use of the above code according to my scenerio. post files has pdf files that i want to limit size. Can you please guide me? – Muhammad Ali Jun 22 '19 at 19:43
  • What part isn't clear about my answer? Did you try applying it to your use-case already? What problem do you have implementing it? – Frank van Puffelen Jun 22 '19 at 23:08
0

Retrofit allows you to upload certain size only at a single time. This site has detailed tutorial from basic to complicated series of articles.

The_Martian
  • 3,684
  • 5
  • 33
  • 61