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`
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With