首页 > 解决方案 > 我想限制上传到 Firebase 存储的 pdf 文件大小。我希望该用户可以上传最大大小为 7MB 的文件

问题描述

我想限制 Firebase 存储。也就是说,用户可以上传最大大小为 7MB 的 pdf 文件。以及如何减小 pdf 文件的大小?有什么办法在android中。

我不知道我的问题的解决方案。我找到了很多,但没有得到我的答案。有人可以帮我制定 Firebase 安全规则吗?我对此不熟悉,找不到任何答案。

标签: javaandroidfirebase-storage

解决方案


Firebase 有关安全规则的文档有一个示例,该示例显示(除其他外)如何设置可以存储的文件的最大大小:

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

由于这些规则只会在实际上传完成后应用(但在文件实际存储在存储桶中之前),因此您通常还需要在上传之前检查 Android 应用中的文件大小,以防止占用带宽对于太大的文件。有关如何在 Android 上检查本地文件大小的一些示例,请参阅在 android sdk 中获取文件大小?


推荐阅读