如何解决使用 node.js 在 Firebase 存储中的文件夹中上传文件
我是上传文件的新手,我想将不同产品的图像上传到 Firebase 存储和应用程序所需的另一个文件中,一个产品可以有多个图像,所以我想为每个产品创建一个文件夹,文件夹的名称将是产品的 id。
在代码中:我使用 @google-cloud/storage 库将文件上传到 firebase 存储,但我在文档中搜索,无法创建文件夹然后将其上传到文件夹。
这里是我的代码:
我创建了 multer 的中间件以将其传递到端点,并带有检查类型的文件。
const express = require("express");
const Multer = require("multer");
const { Storage } = require("@google-cloud/storage")
const storage = new Storage({
projectId: process.env.PROJECT_FIREBASE_ID,keyFilename: "hawat-service.json",});
const bucket = storage.bucket(process.env.BUCKET_NAME);
const multer = Multer({
storage: Multer.memoryStorage(),fileFilter: (req,file,cb) => {
checkFileType(req,cb);
}
})
const checkFileType = (req,cb) => {
if (file.fieldname == 'cover' || file.fieldname == 'images') {
if (!file.originalname.match(/\.(jpg|JPG|jpeg|JPEG|png|PNG|gif|GIF)$/)) {
req.error = new Error( "Only images are allowed")
return cb(null,false);
}
} else if (file.fieldname == 'card' || file.fieldname == 'licence') {
if (!file.originalname.match(/\.(pdf|jpg|JPG|jpeg|JPEG|png|PNG|gif|GIF)$/)) {
req.error = new Error("Only images and pdf are allowed")
return cb(null,false);
}
}
return cb(null,true)
}
module.exports = (req,res,next) => {
return multer.fields([{ name: 'cover',maxCount: 1 },{ name: 'images',maxCount: 5 },{ name: 'card',{ name: 'licence',maxCount: 1 }
])
(req,() => {
if (req.error) return res.status(400).send( {message : req.error.message })
next()
})
}
const express = require("express");
const Multer = require("multer");
const { Storage } = require("@google-cloud/storage");
const storage = new Storage({
projectId: process.env.PROJECT_FIREBASE_ID,});
const bucket = storage.bucket(process.env.BUCKET_NAME);
module.exports = {
upload: async ( file) => {
return new Promise((resolve,reject) => {
let newFileName = `${file.originalname}_${Date.Now()}`;
let fileUpload = bucket.file(newFileName);
const createStream = fileUpload.createWriteStream({
Metadata: {
contentType: file.mimetype
}
});
createStream.on('error',(error) => {
console.log("error in uploading is",error)
reject('Something is wrong! Unable to upload at the moment.');
});
createStream.on('finish',() => {
// The public URL can be used to directly access the file via HTTP.
const url = `https://storage.googleapis.com/${bucket.name}/${fileUpload.name}`;
// storage.bucket(process.env.BUCKET_NAME).file(fileUpload.name).makePublic();
resolve(url);
});
createStream.end(file.buffer);
});
端点是
router.post('/add-product',auth,multer,seller.onAddProduct)
onAddProduct 函数是一个可以接收来自用户的多个文件的函数。
解决方法
Google Cloud Storage 中的文件夹并不是真正的东西。正如您在此 documentation 中看到的:
gsutil 在 Cloud Storage 服务支持的“平面”名称空间之上提供分层文件树的错觉。对于服务而言,对象 gs://your-bucket/abc/def.txt
只是一个名称中碰巧包含 "/"
字符的对象。没有"abc"
目录,只有一个具有给定名称的对象
因此,您在 Cloud Storage 中看到的文件夹只是另一个模拟文件夹结构的对象,真正重要的是对象路径。
就您而言,有两种方法可以完成您想做的事情,您可以:
-
通过创建一个以斜杠结尾的对象来创建一个模拟的空目录。例如,要在存储桶的根目录创建一个名为
foo
的子目录,您需要创建一个名为foo/
的空对象(大小为 0),然后上传带有完整路径的文件。 -
只需上传包含所需“子目录”的完整路径的文件,当您从 GCS 获取它时,它看起来就像位于模拟目录中。
我个人会使用后者,因为只需 1 步而不是 2 步即可获得相同的结果。
,我使用的方法与您使用的方法不同,但您可以使用我的解决方案作为案例研究
await storage.bucket(bucketName).upload(filename,{
destination:"{Foldername}/{Filename}",})
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。