繁体   English   中英

Firebase存储规则“未经授权”

[英]Firebase storage rules “unauthorized”

这些是我的存储规则:

rules_version = '2';
service firebase.storage {
  match /b/{bucket}/o {
    match /auction-images/{auctionId} {
      allow read;
      allow write: if request.auth != null && request.resource.size < 1 * 1024 * 1024
                   && request.resource.contentType.matches('image/.*');

    }
  }
}

如您所见,我想允许所有来自auction-images/{auctionId}读取。 当我尝试运行模拟器时,它按预期工作。

但是,当我启动应用程序并尝试加载图像时,它会显示以下错误:

code: "storage/unauthorized"
code_: "storage/unauthorized"
message: "Firebase Storage: User does not have permission to access 'auction-images/NVOi2jGlfDA5huPUzjSA'."
message_: "Firebase Storage: User does not have permission to access 'auction-images/NVOi2jGlfDA5huPUzjSA'."
name: "FirebaseError"
name_: "FirebaseError"
serverResponse: "{↵  "error": {↵    "code": 403,↵    "message": "Permission denied. Could not perform this operation"↵  }↵}"
serverResponse_: "{↵  "error": {↵    "code": 403,↵    "message": "Permission denied. Could not perform this operation"↵  }↵}"

即使我发布了对规则的更改,它们似乎也没有生效。 我想念什么?

这是给我一个错误的代码:

this.firestorage.storage.ref(`auction-images/${this.recentAuctions[0].uuid}`).list().then(res => {
    console.log(res)
  })
  .catch(err => {
    console.log(err);
  });

原来我的规则是错误的。 我将它们更新为:

rules_version = '2';
service firebase.storage {
  match /b/{bucket}/o {
    match /auction-images/{auctionId}/{image} {
      allow read;
      allow write: if request.auth != null && request.resource.size < 1 * 1024 * 1024
                   && request.resource.contentType.matches('image/.*');
    }
  }
}

我认为match /auction-images/{auctionId}可以为整个文件夹编写规则,但是我必须添加/{image}才能将规则应用于内部图像。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM