【问题标题】:Store in two different folders with multer-storage-cloudinary使用 multer-storage-cloudinary 存储在两个不同的文件夹中
【发布时间】:2021-11-29 05:14:18
【问题描述】:

我想将用户的头像存储在与 Cloudinary 中产品文件夹不同的文件夹中。

我试图这样做,但它不起作用。 如何在同一个项目中使用两个不同的文件夹?

云配置:

const cloudinary = require('cloudinary').v2;

const { CloudinaryStorage } = require('multer-storage-cloudinary');
const multer = require('multer')

 cloudinary.config ({
    cloud_name: process.env.CLOUDINARY_CLOUD_NAME,
    api_key: process.env.CLOUDINARY_KEY,
    api_secret: process.env.CLOUDINARY_SECRET
})


// PARA ALMACENAR LAS IMAGENES DE LOS PRODUCTOS
 const storage = new CloudinaryStorage ({
    cloudinary: cloudinary,
    params:{
        folder: 'ProductosMarketV2',
        allowedFormats : ['jpeg', 'png', 'jpg'],
        // transformation: [{ width: 640, height: 480, crop:'fit'}],
    }
    
});

const parser = multer({storage})


//PARA ALMACENAR LAS IMAGENES DE LOS AVATARES DE USUARIOS
const storage2 = new CloudinaryStorage ({
    cloudinary: cloudinary,
    params:{
        folder: 'ProductosMarketV2/AvataresUsuarios',
        allowedFormats : ['jpeg', 'png', 'jpg'],
        // transformation: [{ width: 640, height: 480, crop:'fit'}],
    }
    
});

const parser2 = multer({storage2})

module.exports = {
    cloudinary,
    storage,
    parser,
    parser2
}

parser 如果我与头像或产品一起使用并将图像保存在 MongoDB 中的正确模型中,则可以使用。但是如果我用parser2,就不行了。

在我的路由中,parser 工作并将 url 和文件名存储在 mongodb 头像模型中:

router.post('/avatar/:id',
auth,
parser.single('imagesAvatar'),
users.crearAvatar
);

使用 parser2,不起作用:

router.post('/avatar/:id',
auth,
parser2.single('imagesAvatar'),
users.crearAvatar
);

感谢您的帮助。

【问题讨论】:

  • 当你提到它不起作用时,你能分享更多关于这个的信息吗?使用 parser2 时是否遇到任何错误,这部分代码是否真的被调用了?即使使用两个解析器,图像是否总是上传到“ProductosMarketV2”文件夹中?
  • 这行不通。我有两种配置,当我尝试使用 de parser2 时,不会将图像发送到 cloudinary、存储和返回数据(url 和文件名),但如果我使用解析器,则效果很好。我想分别存储用户的头像和产品的图像......有帮助吗?谢谢

标签: mongodb express cloudinary


【解决方案1】:

我确实找到了解决方案:

您可以设置云数据:

const cloudinary = require('cloudinary').v2;

const { CloudinaryStorage } = require('multer-storage-cloudinary');
const multer = require('multer')


 cloudinary.config ({
    cloud_name: process.env.CLOUDINARY_CLOUD_NAME,
    api_key: process.env.CLOUDINARY_KEY,
    api_secret: process.env.CLOUDINARY_SECRET
})


// PARA ALMACENAR LAS IMAGENES DE LOS PRODUCTOS
 const storage = new CloudinaryStorage ({
    cloudinary: cloudinary,
    params:{
        folder: 'ProductosMarketV2',
        allowedFormats : ['jpeg', 'png', 'jpg'],
        // transformation: [{ width: 640, height: 480, crop:'fit'}],
    }
    
});


//PARA ALMACENAR LAS IMAGENES DE LOS AVATARES DE USUARIOS
const storage2 = new CloudinaryStorage ({
    cloudinary: cloudinary,
    params:{
        folder: 'ProductosMarketV2/AvataresUsuarios',
        allowedFormats : ['jpeg', 'png', 'jpg'],
        // transformation: [{ width: 640, height: 480, crop:'fit'}],
    }
    
});

const parser = multer({storage: storage})

//const parser2 = multer({storage2})

module.exports = {
    cloudinary,
    storage,
    storage2,
    parser
}

然后在路由中,改为使用:

const { parser,  storage2} = require ('../cloudinary')
const multer = require('multer')

router.put("/editar/:id", 
    auth, 
    parser.single("imagesAvatar"),//with the folder name in the configuration of cloudinary
    users.editarUsuario
    );

//use multer({storage: 'the folder name')} and allways works
    
router.put("/editar/:id", 
    auth, 
    multer({storage: storage2}).single("imagesAvatar"),
    users.editarUsuario
    );

【讨论】:

    猜你喜欢
    • 2020-12-18
    • 2018-07-28
    • 2015-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-02
    • 2018-11-11
    • 1970-01-01
    相关资源
    最近更新 更多