4
votes

I'm trying to upload a audio file to my Firebase Storage Bucket.
There is a folder inside the bucket with the user_id, it should upload the file to this specific folder but the catch is that only that user should be able to access his own folder . I looked in to the documentation and set a rule accordingly, but i seem to be running into a permission issue

{  "error": {    "code": 403,    "message": "Permission denied. Could not perform this operation"  }}

My Rule

service firebase.storage {
match /b/{bucket}/o {
  match /{userId} {
    match /{allPaths=**}{
     allow read, write: if request.auth.uid == userId;
    } 
   }
 }
}

Code Snip

String userUid = FirebaseAuth.getInstance().getCurrentUser().getUid();
StorageReference storageRef = FirebaseStorage.getInstance().getReference();
final StorageReference audioRef = storageRef.child("/ "+ userUid +"/"+filename());

 //rest of upload task here..

The Upload code works perfectly when i set the rule to public access. Can someone help me out to figure out whats wrong with the rule i've written?

1

1 Answers

5
votes

You need to add firebase authentication for this.After that add this permission. Refer this link Firebase Security Rules for Cloud Storage Reference

Came to conclusion that your are missing your Firebase storage bucket name.

service firebase.storage {
match /b/{Pass your firebase storage bucket name here}/o {
  match /{userId} {
    match /{allPaths=**}{
     allow read, write: if request.auth.uid == userId;
    } 
   }
 }
}