代码之家  ›  专栏  ›  技术社区  ›  krv

设置Firebase存储文件大小和文件编号限制

  •  2
  • krv  · 技术社区  · 6 年前

    我正在成功上载 图像 使用AngularFire2进行Firebase存储。

    我有以下上传代码。

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

    我想解决几个问题。

    1. 如何设置文件大小限制?这意味着我希望用户能够上传小于10mb的文件。
    2. 如何限制文件号?这意味着我希望一个用户只能上传3个文件。

    如果没有firebase服务器大小的解决方案,请建议一些客户端大小的解决方案。

    谢谢

    1 回复  |  直到 6 年前
        1
  •  2
  •   Frank van Puffelen    6 年前

    要限制上载的大小,请参见 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
         }
       }
     }
    }
    

    无法使用安全规则限制文件的数量,因此您必须查看以下解决方法: