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

问题描述 投票:0回答:1

根据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',
  },
});

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

我希望能够做这样的事情


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

node.js firebase express google-cloud-storage firebase-admin
1个回答
0
投票

由于Firebase Admin SDK只包含Cloud SDK,因此您可以使用Cloud Storage node.js API documentation作为参考来查看它可以执行的操作。

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

© www.soinside.com 2019 - 2024. All rights reserved.