最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to access a non-default Firestore Database in Firebase Storage Rules? - Stack Overflow

programmeradmin5浏览0评论

When writing rules for Firebase Storage, I get a warning that the parameter for firestore.get must always start with /databases/(default)/documents, like this:

firestore.get(/databases/(default)/documents)

Additionally, according to the warning in the Firebase documentation, multiple databases must be enabled to access documents from the default Cloud Firestore database.

Currently, I have created both a default Firestore database and a custom database. Previously, I was only using the custom database.

When I use firestore.get(/databases/(default)/documents), the security rules work fine. However, since all my existing data is stored in the custom database, I need to access it instead.

How should I write the firestore.get rule to properly access my custom Firestore database instead of the default one?

  • Full Security Rule

Here is the complete security rule I am using:

service firebase.storage {
  match /b/{bucket}/o {
    function findCourse(course) {
      return course in firestore.get(/databases/(default)/documents/accounts/$(request.auth.uid)).data.authedCourse;
    }
    match /video/{course}/{allPaths=**} {
      allow read: if request.auth != null && findCourse(course);
    }
  }
}

The rule allows read access only if the authedCourse array contains the course value.

  • My Attempts
  1. firestore.get(/databases/custom/documents) This results in an error during simulation, stating that I must use /databases/$(database)/documents/.

  2. firestore.get(/databases/$(custom)/documents) This results in the following error:

Function not found error: Name: [firestore.get].; Error: Invalid argument provided to call. Function: [firestore.get], Argument: ["||invalid_argument||"]

  1. firestore.get(/databases/{custom}/documents) This fails to save and produces the following error:
Line 7: Missing 'match' keyword before path.;
Line 7: mismatched input 'creapple' expecting '}';
Line 7: Unexpected '-'.;  
Line 7: Unexpected '}'.;
Line 7: Missing 'match' keyword before path.;
Line 7: Forward slash '/' found where identifier or binding expected.;  
Line 7: mismatched input '$' expecting {'{', '/', PATH_SEGMENT};
Line 9: Unexpected 'match'.

When writing rules for Firebase Storage, I get a warning that the parameter for firestore.get must always start with /databases/(default)/documents, like this:

firestore.get(/databases/(default)/documents)

Additionally, according to the warning in the Firebase documentation, multiple databases must be enabled to access documents from the default Cloud Firestore database.

Currently, I have created both a default Firestore database and a custom database. Previously, I was only using the custom database.

When I use firestore.get(/databases/(default)/documents), the security rules work fine. However, since all my existing data is stored in the custom database, I need to access it instead.

How should I write the firestore.get rule to properly access my custom Firestore database instead of the default one?

  • Full Security Rule

Here is the complete security rule I am using:

service firebase.storage {
  match /b/{bucket}/o {
    function findCourse(course) {
      return course in firestore.get(/databases/(default)/documents/accounts/$(request.auth.uid)).data.authedCourse;
    }
    match /video/{course}/{allPaths=**} {
      allow read: if request.auth != null && findCourse(course);
    }
  }
}

The rule allows read access only if the authedCourse array contains the course value.

  • My Attempts
  1. firestore.get(/databases/custom/documents) This results in an error during simulation, stating that I must use /databases/$(database)/documents/.

  2. firestore.get(/databases/$(custom)/documents) This results in the following error:

Function not found error: Name: [firestore.get].; Error: Invalid argument provided to call. Function: [firestore.get], Argument: ["||invalid_argument||"]

  1. firestore.get(/databases/{custom}/documents) This fails to save and produces the following error:
Line 7: Missing 'match' keyword before path.;
Line 7: mismatched input 'creapple' expecting '}';
Line 7: Unexpected '-'.;  
Line 7: Unexpected '}'.;
Line 7: Missing 'match' keyword before path.;
Line 7: Forward slash '/' found where identifier or binding expected.;  
Line 7: mismatched input '$' expecting {'{', '/', PATH_SEGMENT};
Line 9: Unexpected 'match'.
Share Improve this question edited 30 mins ago Frank van Puffelen 599k85 gold badges889 silver badges859 bronze badges Recognized by Google Cloud Collective asked 1 hour ago EullEull 11 silver badge New contributor Eull is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
Add a comment  | 

1 Answer 1

Reset to default 3

From the documentation you can only access the default firestore database in storage rules.

Warning: Storage Rules can only access documents from the default Cloud Firestore database when multiple databases are active.

发布评论

评论列表(0)

  1. 暂无评论