使用 Admin SDK 将文件上传到 Firebase 存储

Chi*_*ous 12 node.js express google-cloud-storage firebase firebase-admin

根据Docs,我必须将文件名传递给函数才能上传文件。

// Uploads a local file to the bucket
await storage.bucket(bucketName).upload(filename, {
  // Support for HTTP requests made with `Accept-Encoding: gzip`
  gzip: true,
  metadata: {
    // Enable long-lived HTTP caching headers
    // Use only if the contents of the file will never change
    // (If the contents will change, use cacheControl: 'no-cache')
    cacheControl: 'public, max-age=31536000',
  },
});
Run Code Online (Sandbox Code Playgroud)

我在我的服务器端代码中使用 Firebase Admin SDK (Nodejs),客户端以我作为文件对象获取的表单数据发送文件。当函数只接受通向文件路径的文件名时,我如何上传它。

我希望能够做这样的事情


app.use(req: Request, res: Response) {
 const file = req.file;
// upload file to firebase storage using admin sdk
}

Run Code Online (Sandbox Code Playgroud)

Dou*_*son 6

由于 Firebase Admin SDK 只是包装了 Cloud SDK,您可以使用Cloud Storage node.js API 文档作为参考,看看它可以做什么。

您不必提供本地文件。您还可以使用节点流上传。有一个方法File.createWriteStream()可以让您使用 WritableStream 。还有File.save()接受多种事物,包括缓冲区。有使用每种方法的例子在这里