如何在Firebase Storage规则中访问非默认Firestore数据库?
访问自定义Firestore数据库的Storage规则写法
要在Firebase Storage规则中访问自定义Firestore数据库,只需将firestore.get路径里的(default)替换为你的实际自定义数据库ID即可,路径格式为:
firestore.get(/databases/你的自定义数据库ID/documents/目标文档路径)
修改后的完整安全规则示例
假设你的自定义数据库ID是custom-db,修改后的规则如下:
service firebase.storage { match /b/{bucket}/o { function findCourse(course) { // 替换为你的自定义数据库ID return course in firestore.get(/databases/custom-db/documents/accounts/$(request.auth.uid)).data.authedCourse; } match /video/{course}/{allPaths=**} { allow read: if request.auth != null && findCourse(course); } } }
你之前尝试方法的问题说明
- 方法1:如果
custom不是你创建的自定义数据库的实际ID,或者路径未补全到具体文档,会导致格式错误; - 方法2:
$(custom)是规则中的变量引用语法,但这里不需要变量,直接写入实际数据库ID即可,用变量会被识别为无效参数; - 方法3:
{custom}是match语句中的路径绑定语法,不能用于firestore.get的路径参数,因此会触发语法错误。
内容的提问来源于stack exchange,提问作者Eull
相关产品推荐
相关产品推荐

