错误:尝试上传大图像文件时未找到存储/对象

Fir*_*ser 3 google-cloud-storage firebase firebase-storage rxfire

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

\n\n

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

\n\n

我用小图像(可能是 100kb...)进行了测试,效果很好。

\n\n

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

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

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

\n\n

实际结果:错误

\n\n
Uncaught t\xc2\xa0{code_: "storage/object-not-found", message_: "Firebase . \nStorage: Object \'uploads/test/7xpbilmb.jpeg\' does not exist.", \nserverResponse_: "{\xe2\x86\xb5  "error": {\xe2\x86\xb5    "code": 404,\xe2\x86\xb5    "message": \n"Not Found.  Could not get object"\xe2\x86\xb5  }\xe2\x86\xb5}", name_: "FirebaseError"}\n
Run Code Online (Sandbox Code Playgroud)\n

T. *_*yya 5

你可以用两种方法来做。

  1. 承诺

     storageRef.put(blob, {customMetadata}).then(data => {
                    data.ref.getDownloadURL().then(url => {
                        // do whatever you want with url
                    });
                });
    
    Run Code Online (Sandbox Code Playgroud)
  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();
    
    Run Code Online (Sandbox Code Playgroud)

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

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