根据Docs https://cloud.google.com/storage/docs/uploading-objects#storage-upload-object-nodejs,我必须将文件名传递给函数才能上传文件。
// 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
}
由于 Firebase Admin SDK 仅封装了 Cloud SDK,因此您可以使用Cloud Storage Node.js API 文档 https://googleapis.dev/nodejs/storage/latest/作为参考,看看它能做什么。
您不必提供本地文件。您还可以使用节点流上传。有一个方法文件.createWriteStream() https://cloud.google.com/nodejs/docs/reference/storage/2.3.x/File#createWriteStream这让你可以使用 WritableStream。还有文件.save() https://cloud.google.com/nodejs/docs/reference/storage/2.3.x/File#save它接受多种事物,包括缓冲区。每种方法都有使用示例here https://github.com/googleapis/google-cloud-node/issues/2334.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)