我想为Firebase / Storage创建基于用户的安全性。如果我只上传图像,下面允许写入很好。但它可以防止删除照片。如何为此案例创建适当的安全规则?
service firebase.storage {
match /b/<bucket>/o {
match /{allPaths=**} {
allow read: if request.auth != null;
}
match /users/{uid}/{filename} {
allow write: if isCurrentUser(uid);
allow write: if isImage() &&
isCurrentUser(uid) &&
lessThanNMegabytes(n) &&
request.resource !=null &&
filename.size() < 50;
}
}
}
function isCurrentUser(uid) {
return request.auth.uid == uid;
}
function lessThanNMegabytes(n) {
return request.resource.size < n * 1024 * 1024;
}
function isImage() {
return request.resource.contentType.matches("image/.*");
}
答案 0 :(得分:9)
我会用它来检查你是创建/更新文件还是删除它
match /users/{uid}/{filename} {
allow write: if isCurrentUser(uid);
allow write: if resource == null ||
( isImage() &&
lessThanNMegabytes(n) &&
request.resource !=null &&
filename.size() < 50 );
}