如何使用getDownloadURL()(带有令牌)获取完整的URL?

时间:2019-06-19 16:36:30

标签: angular typescript firebase

未处理的承诺拒绝:FirebaseStorageError。{code_:“存储/未找到对象”,message _:“ Firebase存储:对象'k91a73uzb99'不存在。”,serverResponse _:“ {↵错误”:{↵“代码“:404,↵” message“:”否…没有获取对象“,↵” status“:” GET_OBJECT“↵}↵}”,name_:“ FirebaseError”}

export class ReportComponent implements OnInit {

  ref: AngularFireStorageReference;
  task: AngularFireUploadTask;
  newUrl: Observable<string>;

  constructor(private firesStore: AngularFireStorage,
              ) { }

  ngOnInit() { }

  async onSelectFile(event) {
    const id = Math.random().toString(36).substring(2);

    this.ref = this.fireStore.ref(id);
this.task = this.ref.put(event.target.files[0]); /*
        .snapshotChanges()
            .subscribe( (value) => {
            }); */

this.uploadPercent = this.task.percentageChanges();
// get notified when the download URL is available
this.task.snapshotChanges().pipe(
      finalize(() => {
        this.newUrl = this.ref.getDownloadURL();
        console.log(this.newUrl);
      })
).subscribe();

}

预期: https://firebasestorage.googleapis.com/v0/b/ / o / ?alt = media&token = <........>

实际: https://firebasestorage.googleapis.com/v0/b/ / o /

1 个答案:

答案 0 :(得分:0)

错误消息显示“存储/未找到对象”和“对象'k91a73uzb99'不存在”。这意味着您要使用以下代码行上传文件:

this.ref = this.firesStore.ref(id);
this.task = this.ref.put(event.target.files[0]);

尚未完成上传。您尝试在上传完成之前获取其下载URL:

this.newUrl = await this.firesStore.ref(id).getDownloadURL();

您将要遵循使用返回的AngularFireUploadTask的example code from the documentation来等待上载完成,然后再访问其下载URL。因此,它将如下所示:

this.uploadPercent = task.percentageChanges();
// get notified when the download URL is available
task.snapshotChanges().pipe(
    finalize(() => this.newUrl = this.ref.getDownloadURL() )
 )
.subscribe()