使用 Admin SDK 将文件上传到 Firebase 存储 [英] Upload File to Firebase Storage using Admin SDK

查看:32
本文介绍了使用 Admin SDK 将文件上传到 Firebase 存储的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

According to the Docs, I have to pass the filename to the function in order to upload a file.

// 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),客户端以我作为文件对象获得的表单数据发送文件.当函数只接受导致文件路径的文件名时,我该如何上传.

I am using Firebase Admin SDK (Nodejs) in my server side code and clients send file in form-data which i get as File Objects. How then do i upload this when the function accepts only filename leading to filepath.

我希望能够做这样的事情

I want to be able to do something like this


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

推荐答案

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

Since the Firebase Admin SDK just wraps the Cloud SDK, you can use the Cloud Storage node.js API documentation as a reference to see what it can do.

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

You don't have to provide a local file. You can also upload using node streams. There is a method File.createWriteStream() which gets you a WritableStream to work with. There is also File.save() which accepts multiple kinds of things, including a Buffer. There are examples of using each method here.

这篇关于使用 Admin SDK 将文件上传到 Firebase 存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆