Firebase存储:用户无权访问“路径”

问题描述 投票:0回答:1

我正在尝试使用angularfire2将文件上传到firebase存储。

这是我的代码:

 public pushUploadAudio(upload: AudioFile){

   let id = this.afs.createId();
   let storageRef = firebase.storage().ref();
  //  the audio file will be uploaded to the id generated to the Podcast Document
   let uploadTask = storageRef.child(`audio_files/${id}`).put(upload.file);
   uploadTask.on(firebase.storage.TaskEvent.STATE_CHANGED,
    (snapshot) =>{
      // upload in progress
      upload.progress = Math.floor((uploadTask.snapshot.bytesTransferred / uploadTask.snapshot.totalBytes )* 100)
    },
    (error) =>{
      // upload failed
      this.flashMessagesService.show('Oh snap! please try again..',  { cssClass: 'alert alert-danger', timeout: 1500 })      
      console.log(error)
    },
    () => {
      // upload success
      upload.url = uploadTask.snapshot.downloadURL;
          //  upload.name is the name ref in firebase storage
      upload.name = uploadTask.snapshot.ref.name;
      upload.podcast_id = id;
      this.flashMessagesService.show('File was successfuly uploaded!',  { cssClass: 'alert alert-success', timeout: 1500 })

    }
  )
 }

存储规则:

service firebase.storage {
  match /image_files {
    match /{allPaths=**} {
      allow read, write: if request.auth != null;
    }
  }
}

我收到以下错误:

FirebaseStorageError {code_: "storage/unauthorized", message_: "Firebase Storage: User does not have permission to access 'audio_files/WKkEKp4o5BUYOt5J4vsX'.", serverResponse_: "{↵  "error": {↵    "code": 403,↵    "message": "Pe…n denied. Could not perform this operation"↵  }↵}", name_: "FirebaseError"}

如果我使存储规则是allow read, write: if true = true;然后它上传。

angular firebase firebase-storage angularfire2
1个回答
0
投票

确保用户已登录,因为如果auth为false,则根据您的规则拒绝写入。例如,来自Firebase documentation

firebase.auth().signInAnonymously().catch(function(error) {
  // Handle Errors here.
  var errorCode = error.code;
  var errorMessage = error.message;
  // ...
});
© www.soinside.com 2019 - 2024. All rights reserved.