【问题标题】:Error: storage/object-not-found when trying to upload large image file错误:尝试上传大图像文件时找不到存储/对象
【发布时间】:2019-01-20 17:13:17
【问题描述】:

尝试使用 RxFire 在 Google Cloud Storage 中上传大图像文件时出现错误:storage/object-not-found。

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

我用小图像(可能是 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('error', 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"}

【问题讨论】:

  • 我猜 getDownloadURL 在上传完全完成之前就被调用了。
  • 嘿,是的,这很奇怪.. 使用 8kb 文件,它可以工作,但是当我达到 300kb 左右时,就会出现此错误。我正在等待传输的字节数 === 总字节数.. 但也许这还不够?
  • 使用常规 javascript APIS,您等待 UploadTask 承诺解决,或者等待 UploadTaskSnapshot 状态为“SUCCESS”。
  • 好吧,我现在使用常规的 .put() 方法使用 Promise,它工作正常......谢谢。但是如果有人对 RxJs 有同样的问题,请随时分享:D
  • AngularFireStorageReferenceAngularFireUploadTask 提到不同的文件名时,我遇到了这个问题,因为我的错误包括其中一个时间戳,而另一个没有。最后,const fileName = file.name + '_' + Date.now(); 等变量声明和AngularFireStorageReferenceAngularFireUploadTask 创建的这两个对象中的引用起到了作用。

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


【解决方案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();
    

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

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

【讨论】:

    猜你喜欢
    • 2023-02-24
    • 2022-01-08
    • 2019-12-18
    • 1970-01-01
    • 2017-01-11
    • 2019-10-19
    • 1970-01-01
    相关资源
    最近更新 更多