I'm building an image upload app using multer and cloudinary. I've configured my environment variables properly using dotenv. However, when I try to upload files using Cloudinary I get the error "Must supply api_key".
The Cloudinary API credentials are correctly supplied and provisioned as in the code below:
cloudinary.js
const cloudinary = require('cloudinary');
cloudinary.config({
cloud_name: process.env.CLOUDINARY_CLOUD_NAME,
api_key: process.env.CLOUDINARY_API_KEY,
api_secret: process.env.CLOUDINARY_API_SECRET
})
exports.uploads = (file, folder) => {
return new Promise(resolve => {
cloudinary.uploader.upload(file, (result) => {
resolve({
url: result.url,
id: result.public_id
})
}, {
resource_type: "auto",
folder: folder
})
})
}
.env
CLOUDINARY_CLOUD_NAME='my_cloudinary_cloud_name'
CLOUDINARY_API_KEY='my_cloudinary_api_key'
CLOUDINARY_API_SECRET='my_cloudinary_api_secret'
The .env file is also required correctly in my app.js file:
app.js
require('dotenv').config({
path: './app/config/.env'
});
If I console.log any of the cloudinary config variables inside the cloudinary.js file, I get the expected output, but when I try to use cloudinary in my upload route, I get the error that I must supply api_key. Please can someone help me point out what I'm doing wrong? I don't want to have to write out my cloudinary config variables in the cloudinary.js file because the code is being published to Github.
Here is my post route using cloudinary:
const express = require('express'),
Product = require('../app/models/product'),
upload = require('../app/utils/multer'),
cloudinary = require('../app/utils/cloudinary'),
fs = require('fs'),
router = express.Router();
router.post('/products', upload.array('image'), async (req, res) => {
const uploader = async (path) => await cloudinary.uploads(path, 'Images');
const urls = []
const files = req.files
for (const file of files) {
const {
path
} = file;
const newPath = await uploader(path)
urls.push(newPath)
fs.unlinkSync(path)
}
const name = req.body.name
const product = new Product({
name: name,
imageone: urls[0].url,
imagetwo: urls[1].url,
imagethree: urls[2].url,
imagefour: urls[3].url
})
product.save(function (err, prod) {
if (err) {
throw err
} else {
req.flash('success', "You have added a new product.")
res.redirect("/dashboard")
}
})
})
module.exports = router;
CodePudding user response:
Kóyo @awesome-bassey!
Following cloudinary docs for Nodejs and it's official repo i would advice you to import v2
of the cloudinary API.
In case your issue still remains - please share stack-trace with us
CodePudding user response:
Using the Node.js v2, it can be declared as shown in the following code sample as described in the installation setup document (i.e. require('cloudinary').v2):
var cloudinary = require('cloudinary').v2;
cloudinary.config({
cloud_name: '<YOUR_CLOUD_NAME>',
api_key: '<YOUR_API_KEY>',
api_secret: '<YOUR_API_SECRET>',
secure: true
});
cloudinary.uploader.upload("my_image.jpg",
function(error, result) {
console.log(result, error);
});