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

FireBase存储“不能按大小要求删除”

  •  2
  • Rbar  · 技术社区  · 7 年前

    我希望允许用户只将文件上传到他们自己的存储桶中,最大文件大小为1MB,并且仍然允许他们删除这些文件。我添加了以下内容:

    match /myusers/{userId}/{allPaths=**} {
      allow write: if request.auth.uid == userId && request.resource.size < 1 * 1024 * 1024;
      allow read: if request.auth.uid == userId;
    }
    

    我既在模拟器中测试,也在我的项目中生活。这不允许我删除文档( access denied )如果我删除 && request.resource.size < 1 * 1024 * 1024; 根据上述规则,可以删除文档(但这样不会阻止上载超过1MB的文件)。

    我想可能是因为 request.resource null ,所以我尝试了以下方法:

    match /myusers/{userId}/{allPaths=**} {
      allow write: if request.auth.uid == userId && (request.resource.size < 1 * 1024 * 1024 || request.resource == null);
      allow read: if request.auth.uid == userId;
    }
    

    但是,删除失败,并出现以下错误(在模拟器中):

    错误:Simulator.Rules行[5],列[16]。对象上的属性资源未定义。

    enter image description here

    我已经研究了所有这些解决方案,并尽可能多地修改了规则,但无济于事:

    是否有人知道如何为允许的文件设置最大大小,但仍允许删除?

    2 回复  |  直到 7 年前
        1
  •  2
  •   Rbar    7 年前

    明白了!这是在生产中都有效的 在模拟器中:)

    match /myusers/{userId}/{allPaths=**} {
      allow write: if request.auth.uid == userId && 
                      (request.resource.size < 1 * 1024 * 1024 || request.method == 'delete');
      allow read: if request.auth.uid == userId;
    }
    

    P.S.@Doug Stevenson,如果你能在内部提交一份说明,我想如果 request.method 实例与 delete , update , get 和/或 create 可以添加到文档中!

    编辑:

    而且,这在模拟器和产品中都有效(在我看来,这比第一个选项更易读)。感谢@doug stevenson!

    match /myusers/{userId}/{allPaths=**} {
      allow write: if request.auth.uid == userId && request.resource.size < 1 * 1024 * 1024;
      allow delete: if request.auth.uid == userId 
      allow read: if request.auth.uid == userId;
    }
    
        2
  •  1
  •   Doug Stevenson    7 年前

    据我所见,这只是控制台模拟器中的一个错误。我根据这些规则编写了实际的(Android)应用程序代码,并且能够创建和删除内容,没有任何问题。问题是模拟器不理解如何解释 request.resource == null 结果就是失败了。

    我将在内部为此提交一个bug报告,但是如果某些东西没有意义,您应该始终对照实际产品检查您的规则,因为很难保持模拟器完全同步。