Uploads file on Cloudinary with Node.js: Step-by-Step Guide

Uploads file on Cloudinary with Node.js: Step-by-Step Guide

1st step: Create an account on cloudinary.com and get the three things Name, Key, and Secret. Cloudinary is a free.

2nd step: Create a file on your node js project and this code.

// uploadOnCloudinary.js
import { v2 as cloudinary } from "cloudinary";
import fs from "fs";

cloudinary.config({
  cloud_name: process.env.CLOUDINARY_NAME,
  api_key: process.env.CLOUDINARY_API_KEY,
  api_secret: process.env.CLOUDINARY_API_SECRET,
});

const uploadOnCloudinary = async (locationFilePath) => {
  try {
    if (!locationFilePath) return null;
    const response = await cloudinary.uploader.upload(locationFilePath, {
      resource_type: "auto",
      folder: "my_folder",
    });
    // File Upload Successfully
    console.log("File Upload Successfully");
    return response;
  } catch (error) {
    fs.unlinkSync(locationFilePath);
    throw error;
  }
};

export default uploadOnCloudinary;

Whenever you want to upload a file on Cloudinary call this function uploadOnCloudinary to pass the path of your file like that.

import uploadOnCloudinary from "../utils/cloudinary.js"; //Import  uploadOnCloudinary function

const avatarUpdate = async () => {
const avatar = await uploadOnCloudinary("You file path");
});

Explanation the Code:

import { v2 as cloudinary } from "cloudinary";
import fs from "fs";
  • The cloudinary.config() function is used to configure the Cloudinary SDK with the necessary credentials to authenticate and access the Cloudinary service.
cloudinary.config({
  cloud_name: process.env.CLOUDINARY_NAME,
  api_key: process.env.CLOUDINARY_API_KEY,
  api_secret: process.env.CLOUDINARY_API_SECRET,
});
  • The line if (!locationFilePath) returns null; is checking if the locationFilePath parameter is falsy (empty, null, undefined, etc.). If it is false, it means that no file path was provided, so the function immediately returns null. This is a way to handle cases where the locationFilePath parameter is missing or invalid, preventing further execution of the function.

  • The code is using the Cloudinary SDK's uploader.upload() method to upload a file to the Cloudinary service.

 const response = await cloudinary.uploader.upload(locationFilePath, {
      resource_type: "auto",
      folder: "my_folder",
    });
  • The resource_type: "auto" property is used in the cloudinary.uploader.upload() method to automatically determine the resource type of the uploaded file.

  • The line folder: "my_folder", specifies the folder in which the uploaded file will be stored on Cloudinary. By setting the folder property to "my_folder", the uploaded file wi be placed in a folder named "my_folder" within the Cloudinary account. This helps i organizing and managing files within the Cloudinary service.

  • The line return response; returns the response object from the Cloudinary API after the file has been successfully uploaded. The response object contains information about the uploaded file, such as the public URL, file format, size, and other metadata. By returning the response object, the calling code can access and use this information as needed.

  • The fs.unlinkSync(locationFilePath) function is used to delete a file from the local file system. In this code, it is called within the catch block, which means it will be executed if an error occurs during the file upload process.

  • The line throw error; is used to throw an error and propagate it to the calling code.