Firebase存储–不能删除并符合大小要求 [英] Firebase Storage – Can't delete with size requirement
问题描述
我想允许用户仅将文档上传到他们自己的存储桶中,最大文件大小为1MB,并且仍然允许他们删除文件.我添加了以下内容:
I would like to allow users to only upload documents to their own bucket in storage with a maximum file size of 1MB, and still let them delete the files. I have added the following, which:
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的文件.
I am testing both in the simulator and live in my project. This doesn't let me delete the document (access denied
). If I remove && request.resource.size < 1 * 1024 * 1024;
from the rule above, the the document can be deleted (but then won't prevent upload of files greater than 1MB.
我认为可能是因为request.resource
是null
而拒绝了它,所以我尝试了以下操作:
I thought maybe it was rejecting it because request.resource
is null
, so I tried the following:
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;
}
仍然,删除失败,并出现以下错误(在模拟器中):
Still, deleting fails with the following error (in the simulator):
错误:simulator.rules第[5]行,第[16]列.属性资源未在对象上定义.
Error: simulator.rules line [5], column [16]. Property resource is undefined on object.
我已经研究了所有这些解决方案,并按照我能想到的方式修改了规则,但无济于事:
I have looked at all of these solutions and modified the rule as many ways as I can think of, to no avail:
有人知道如何为允许的文件设置最大大小,但仍然允许删除吗?
推荐答案
想通了!这是在生产环境和在模拟器中都有效的方法:)
Figured it out! Here's what works both in production and in the simulator :)
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,如果您能够在内部提交便笺,我想如果具有delete
,update
,get
和/或create
的request.method
示例可以对其他人很有帮助添加到文档中!
p.s. @Doug Stevenson, if you're able to submit a note internally, I imagine that it would be very helpful for others if request.method
examples with delete
, update
, get
, and/or create
could be added to the documentation!
此外,这在模拟器和生产环境中均有效(我认为比第一种方法更具可读性).感谢@Doug Stevenson的提示!
Also, this works in both the simulator and production (and is a bit more readable than the first option, in my opinion). Thanks to @Doug Stevenson on the tip!
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;
}
这篇关于Firebase存储–不能删除并符合大小要求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!