Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract Public Id From Cloudinary URL

I am using Cloudinary to host my media on the cloud for my NodeJS project. To delete an image from the Clodinary Cloud, I need to pass a Public Id for that image, to the Cloudinary API. I realised, Public ID is embedded into the url, how to I extract it out from the URL?

Because, I don't want to store my data in this format :

image : {
    url : `http://res.cloudinary.com/cloud_name/image/upload/v1647610701/rsorl4rtziefw46fllvh.png`,
    publicId : `rsorl4rtziefw46fllvh`
}

Rather, I find it better to store it like this :

image : `http://res.cloudinary.com/cloud_name/image/upload/v1647610701/rsorl4rtziefw46fllvh.png`
like image 666
Aditya Pandey Avatar asked Nov 04 '25 05:11

Aditya Pandey


1 Answers

You can also use the method extractPublicId from the package cloudinary-build-url

import { extractPublicId } from 'cloudinary-build-url'

const publicId = extractPublicId(
    "http://res.cloudinary.com/demo/image/upload/v1312461204/sample.jpg"
  ) 

Documentation: https://cloudinary-build-url.netlify.app/usage/extractPublicId

like image 64
Maya Shavin Avatar answered Nov 06 '25 21:11

Maya Shavin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!