【发布时间】: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