I’m working on an Express.js project where I need to upload images to Cloudinary using multer-storage-cloudinary. My setup involves using dotenv to manage environment variables for Cloudinary credentials. Here’s my code:
<code>import express from "express";
import { v2 as cloudinary } from "cloudinary";
import { CloudinaryStorage } from "multer-storage-cloudinary";
import multer from "multer";
import dotenv from "dotenv";
dotenv.config(); // Load environment variables from .env file
cloud_name: process.env.CLOUDINARY_CLOUD_NAME,
api_key: process.env.CLOUDINARY_API_KEY,
api_secret: process.env.CLOUDINARY_API_SECRET,
const storage = new CloudinaryStorage({
folder: "some-folder-name",
format: async (req, file) => "png", // Optional: Automatically convert images to PNG
public_id: (req, file) => file.originalname.split('.')[0], // Store the file name without extension
const parser = multer({ storage: storage });
app.post("/post", parser.single("image"), async (req, res) => {
const imageUrl = req.file.path || req.file.url || req.file.secure_url;
res.status(201).send({ message: "Product posted successfully", imageUrl });
console.error("Upload Error:", error);
res.status(500).send({ message: "An error occurred during the upload." });
app.listen(4000, () => console.log("listening on port 4000"));
<code>import express from "express";
import { v2 as cloudinary } from "cloudinary";
import { CloudinaryStorage } from "multer-storage-cloudinary";
import multer from "multer";
import dotenv from "dotenv";
dotenv.config(); // Load environment variables from .env file
const app = express();
cloudinary.config({
cloud_name: process.env.CLOUDINARY_CLOUD_NAME,
api_key: process.env.CLOUDINARY_API_KEY,
api_secret: process.env.CLOUDINARY_API_SECRET,
});
const storage = new CloudinaryStorage({
cloudinary: cloudinary,
params: {
folder: "some-folder-name",
format: async (req, file) => "png", // Optional: Automatically convert images to PNG
public_id: (req, file) => file.originalname.split('.')[0], // Store the file name without extension
},
});
const parser = multer({ storage: storage });
app.post("/post", parser.single("image"), async (req, res) => {
try {
const imageUrl = req.file.path || req.file.url || req.file.secure_url;
res.status(201).send({ message: "Product posted successfully", imageUrl });
} catch (error) {
console.error("Upload Error:", error);
res.status(500).send({ message: "An error occurred during the upload." });
}
});
app.listen(4000, () => console.log("listening on port 4000"));
</code>
import express from "express";
import { v2 as cloudinary } from "cloudinary";
import { CloudinaryStorage } from "multer-storage-cloudinary";
import multer from "multer";
import dotenv from "dotenv";
dotenv.config(); // Load environment variables from .env file
const app = express();
cloudinary.config({
cloud_name: process.env.CLOUDINARY_CLOUD_NAME,
api_key: process.env.CLOUDINARY_API_KEY,
api_secret: process.env.CLOUDINARY_API_SECRET,
});
const storage = new CloudinaryStorage({
cloudinary: cloudinary,
params: {
folder: "some-folder-name",
format: async (req, file) => "png", // Optional: Automatically convert images to PNG
public_id: (req, file) => file.originalname.split('.')[0], // Store the file name without extension
},
});
const parser = multer({ storage: storage });
app.post("/post", parser.single("image"), async (req, res) => {
try {
const imageUrl = req.file.path || req.file.url || req.file.secure_url;
res.status(201).send({ message: "Product posted successfully", imageUrl });
} catch (error) {
console.error("Upload Error:", error);
res.status(500).send({ message: "An error occurred during the upload." });
}
});
app.listen(4000, () => console.log("listening on port 4000"));
I’ve logged the req.file object to see what properties are available, but the URL I expect doesn’t seem to be there.
I tried using req.file.path, req.file.url, and req.file.secure_url, but none of them are providing the URL.
and after all no front end is involved and tested it with postman. everything seems fine to me. and also i have changed my ip address to cross any filtering, but nothing happens