Search code examples
node.jscloudinary

Cloudinary, newly uploaded pictures replacing the last as have the same name?


I am having issues where when I upload a file to cloudinary it is getting the filename 'new' that I have given like below;

const storage = new CloudinaryStorage({
    cloudinary: Cloudinary,
    params: {
      folder: 'linkedIn',
      format: async (req, file) => 'png', // supports promises as well
      public_id: (req, file) => `new`,
    },
  })

The issue is it is replacing any pictures with that name with the most recently uploaded picture.

I'd appreciate any help greatly!


Solution

  • You have a bunch of options Paul.

    Firstly, you don't need to specify the public_id at all because Cloudinary can assign one automatically for your image, which is guaranteed to be unique.

    That being said, if you'd like to have some control over the name, you can specify the public_id to be the same as the name of the file that you're uploading (potentially useful for SEO), furthermore, you can pass in an option as well which will append that filename with a randomly generated string, guaranteeing uniqueness:

    cloudinary.v2.uploader.upload("sample_file.jpg", {
      use_filename: true, 
      unique_filename: false
    },  
    

    This is also covered in this training course (which you can access for free): https://training.cloudinary.com/courses/cloudinary-fundamentals-for-developers.

    You can read more about this here: https://cloudinary.com/documentation/upload_images#public_id.

    ps: what is the npm package that you're using?