错误:尝试上传大型图片文件时出现存储/找不到对象

时间:2019-01-20 17:13:18

标签: firebase google-cloud-storage firebase-storage rxfire

我遇到错误:尝试使用RxFire在Google Cloud Storage中上传大型图像文件时,存储/找不到对象。

他们说在存储桶中找不到该图像,但是当我检查时,我看到了它们!

我用小图像(可能100kb ...)进行了测试。

但是尝试使用> 500kb的图像,但是不起作用...

upload$
  .pipe(
    switchMap((event: any) => {
      const name = Math.random().toString(36).substring(5);
      const blob = event.target.files[0];
      const type = blob.type.replace('image/', '');
      const ref = storage.ref(`uploads/test/${name}.${type}`);
      return put(ref, blob);
    }),
    map(snapshot => snapshot),
    filter(snapshot => snapshot.totalBytes === snapshot.bytesTransferred),
    mergeMap(snapshot => getDownloadURL(snapshot.ref))
  )
  .subscribe(url => {
    console.log('Results', url)
  }, (error) => {
    // ERROR HERE
    console.log('erorr', error)
  })

预期结果:上传可处理大图像

实际结果:错误

Uncaught t {code_: "storage/object-not-found", message_: "Firebase . 
Storage: Object 'uploads/test/7xpbilmb.jpeg' does not exist.", 
serverResponse_: "{↵  "error": {↵    "code": 404,↵    "message": 
"Not Found.  Could not get object"↵  }↵}", name_: "FirebaseError"}

1 个答案:

答案 0 :(得分:1)

您可以同时使用两种方法。

  1. 承诺

     storageRef.put(blob, {customMetadata}).then(data => {
                    data.ref.getDownloadURL().then(url => {
                        // do whatever you want with url
                    });
                });
    
  2. 可观察物

    downloadURL = new Subject();
    this.downloadURL.pipe(
                    map(obs => obs),
                    concatAll()
                ).subscribe(url => {
                    // do whatever you want with url
                });
    
    let task = ref.put(blob, {customMetadata});
    task.snapshotChanges().pipe(
                    finalize(() => this.downloadURL.next(ref.getDownloadURL()))
                ).subscribe();
    

这应该足以使您获得downloadURL。如果您想使用可观察对象跟踪上传进度,请使用以下代码:

task.percentageChanges().subscribe(progress => {
                console.log('upload progress: ', progress);
                if (res >= 100) {
                    // HOORAY!
                }
            });