我想限制上传到 Firebase 存储的 pdf 文件大小.我希望该用户可以上传最大大小为 7MB 的文件



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

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

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 中获取文件大小?

Retrofit 只允许您一次上传特定大小。该网站有从基础到复杂系列文章的详细教程。

最新更新