【问题标题】:Upload file in folder in Firebase storage with node.js使用 node.js 在 Firebase 存储中的文件夹中上传文件
【发布时间】:2021-06-28 12:43:32
【问题描述】:

我是上传文件的新手,我想将不同产品的图像上传到 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, file, cb);
    }
})


const checkFileType = (req ,file, 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', maxCount: 1 },
    { name: 'licence', maxCount: 1 }
])
        (req, res, () => {
            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,
  keyFilename: "hawat-service.json",
});

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 函数是一个可以从用户那里接收多个文件的函数。

那么如何为每个产品创建一个文件夹,然后上传文件夹中的文件?

另外,创建后如何删除文件夹?

【问题讨论】:

  • const newFileName = ${product_id}/${file.originalname}_${Date.now()};

标签: node.js express google-cloud-storage firebase-storage multer


【解决方案1】:

我没有使用与您相同的方法,但您可以将我的解决方案用作案例研究

  await storage.bucket(bucketName).upload(filename, {
        destination:"{Foldername}/{Filename}",
})

【讨论】:

    【解决方案2】:

    Google Cloud Storage 中的文件夹并不是真正的东西。正如您在documentation 中看到的那样:

    gsutil 在云存储服务支持的“平面”名称空间之上提供了分层文件树的错觉。对于服务而言,对象gs://your-bucket/abc/def.txt 只是一个名称中恰好包含"/" 字符的对象。没有"abc"directory,只有一个具有给定名称的对象

    因此,您在 Cloud Storage 中看到的文件夹只是另一个模拟文件夹结构的对象,真正重要的是对象路径。

    在你的情况下,你可以通过两种方式去做你想做的事情,你可以:

    • 通过创建一个以斜杠结尾的对象来创建一个模拟的空目录。例如,要在存储桶的根目录创建一个名为 foo 的子目录,您将创建一个名为 foo/ 的空对象(大小为 0),然后使用其完整路径上传文件。

    • 只需上传包含所需“子目录”的完整路径的文件,当您从 GCS 获取它时,它看起来就像位于模拟目录中。

    我个人会使用后者,因为您只需 1 步而不是 2 步即可获得相同的结果。

    【讨论】:

      猜你喜欢
      • 2016-03-23
      • 2020-07-10
      • 2023-03-09
      • 2019-05-26
      • 1970-01-01
      • 1970-01-01
      • 2019-01-26
      • 1970-01-01
      • 2018-09-10
      相关资源
      最近更新 更多