Aditya Pandey
Aditya Pandey

Reputation: 140

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`

Upvotes: 4

Views: 4656

Answers (4)

bayramkazik
bayramkazik

Reputation: 37

The answer from @Aditya Pandey wasn't working in my case because I had the period character in my filename so I had to revise the code:

const extractPublicId = (link: string) => {
  const dottedParts = link.split('/').pop()!.split('.');
  dottedParts.pop();
  return dottedParts.join('.');
};

This code extracts the part from last / to last . character. You don't need the ! after pop() if you aren't using Typescript.

Upvotes: 1

Maya Shavin
Maya Shavin

Reputation: 91

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

Upvotes: 4

orimdominic
orimdominic

Reputation: 1135

Based on the answer by a Cloudinary support team member

... the public_id contains all folders and the last part of the public_id is the filename.

Here is what I tried and worked

const path = require("path");

const getPublicId = (imageURL) => {
  const [, publicIdWithExtensionName] = imageURL.split("upload/");
  const extensionName = path.extname(publicIdWithExtensionName)
  const publicId = publicIdWithExtensionName.replace(extensionName, "")
  return publicId
};

especially for cases where you store your assets in folders

Upvotes: 0

Aditya Pandey
Aditya Pandey

Reputation: 140

The solution to this problem is to implement a funciton which extracts the publicId for every URL passed in as argument.

Here's the function :

const getPublicId = (imageURL) => imageURL.split("/").pop().split(".")[0];

Edited after @loic-vdb 's suggestion

Explanation :

  1. It splits the string in an array using "/" as seperator.

    imageURL="http://res.cloudinary.com/cloud_name/image/upload/v1647610701/rsorl4rtziefw46fllvh.png";

becomes,

imageURL = [ 'http:',
  '',
  'res.cloudinary.com',
  'cloud_name',
  'image',
  'upload',
  'v1647610701',
  'rsorl4rtziefw46fllvh.png' ]
  1. Next, pop the array (returns the last element of the array)

    imageURL = 'rsorl4rtziefw46fllvh.png';

  2. Now, split this string into array using "." as seperator, we get :

    imageURL = [ 'rsorl4rtziefw46fllvh', 'png' ]

  3. Finally select the 0th element that is our PublicId return that

    imageURL = 'rsorl4rtziefw46fllvh';

Upvotes: 2

Related Questions