首页 > 解决方案 > 使用 https 流将文件上传到 Firebase Ddmin SDK (GCS)

问题描述

当尝试将流上传到 Google 存储桶时,我Error: Not Found在使用 get 方法时得到,并且Error: socket hang up在使用 request 方法时会延迟几秒钟。

firebase 的一切似乎都初始化得很好,当我记录流时,我看到数据通过了,但是使用远程 URL 将文件写入 GCS 的最佳方法是什么?

const storage = firebase.storage()

const bucket = storage.bucket("bucket/path")
const file = bucket.file("filename.pdf")

const url =
  "https://url/to/file/filename.pdf"


https.get(url, async (res) => {
  console.log(res)
  res.pipe(file.createWriteStream())
})

标签: javascriptnode.jsfirebasegoogle-cloud-storagefirebase-storage

解决方案


问题的原因是将文件夹路径传递到存储桶名称而不是文件名。

Bucket 名称在存储控制台中可用,不要传入文件夹路径。

存储桶名称示例:( gs://bucket.appspot.com 将 gs:// 作为值传递时删除)

const bucket = storage.bucket("bucketname")
const file = bucket.file("bucket/path/filename.pdf")


推荐阅读