Search code examples
cloudinary

Upload an image to cloudinary with specific public_id to a specific folder (node.js)


I'm trying to upload an image to a specific folder and also set the public_id.

I read the documentation here: https://cloudinary.com/documentation/image_upload_api_reference

My code is:

cloudinary.v2.uploader.upload(
   localImagePath,
   {
      public_id: '/my_folder/my_public_id',
      folder: 'my_folder',
      resource_type: 'image',
      transformation: 'profile_image'
   },
   (err, url) => {
      if (err) return reject(err);
      return resolve(url);
   }
);

But the image is uplaoded to this path: 'my_folder/my_folder/my_public_id' instead of my_folder/my_public_id.

From the docs:

The public ID contains the full path of the uploaded asset, including the folder name

I tried everything, please help!


Solution

  • You're specifying the folder twice - once in the public_id and again in the folder option.

    Given that you are passing both upload options, the resulting public_id of the image will be <folder>/<public_id>.

    You can omit the folder parameter completely and you will be getting your expected result (/my_folder/my_public_id).