【问题标题】:Firebase Storage: User does not have permission to access 'path'Firebase 存储:用户无权访问“路径”
【发布时间】:2018-05-30 17:19:24
【问题描述】:

我正在尝试使用 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】:

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

    firebase.auth().signInAnonymously().catch(function(error) {
      // Handle Errors here.
      var errorCode = error.code;
      var errorMessage = error.message;
      // ...
    });
    

    【讨论】:

    • 我正在使用 googleLogin。 googleLogin() { const provider = new firebase.auth.GoogleAuthProvider(); return this.oAuthLogin(provider); } private oAuthLogin(provider){ return this.afAuth.auth.signInWithPopup(provider) .then((credential)=> { this.updateUserData(credential) this.router.navigate(['/home']) }) }
    猜你喜欢
    • 2018-06-05
    • 2019-04-20
    • 2016-12-04
    • 2021-08-18
    • 2021-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-02
    相关资源
    最近更新 更多