Home > OS >  Firebase Storage Rules with Firestore not working
Firebase Storage Rules with Firestore not working

Time:10-18

I am trying to upload images with Firebase Storage JS SDK with some rules based on Firestore data.

imageList.forEach(element => {

            tasks.push(firebaseStorage.child("Devices").child(dateTime).child(element.name).put(element, { contentType: element.type }))

        });

Rules:

rules_version = '2';
service firebase.storage {
  match /b/{bucket}/o {
    match /Devices/{deviceId} {
    
      // Authentication is required.
      allow create: if request.auth.uid != null && 
      // Only Admins are capable of uploading images.
      firestore.exists(/databases/(default)/documents/Admins/$(request.auth.uid));
      
      // Authentication is required.
      allow read: if request.auth.uid != null && 
      // Only Cashiers are capable of reading images.
      firestore.exists(/databases/(default)/documents/Cashiers/$(request.auth.uid));
      
      // No update or deletion of files in client (web) for now.
      allow update, delete: if false
      
    }
  }
}

This is what I get. Any idea what went wrong here? TIA.

enter image description here

Is read and write permission are both required for uploading?

CodePudding user response:

The match /Devices/{deviceId} matches is applied for the files in /Devices directory only. If the rule must be applied to a file within the deviceId sub directory then the path must be:

match /Devices/{deviceId}/{fileName} {
  // ...
}

If a rule must be recursively applied to all sub directories, then use a wildcard:

match /Devices/{device=**}
  • Related