How to programmatically retrieve public id from URL?

Comments

9 comments

  • Avatar
    Michal Kuperman

    Hi Peters,

    We don't have an API to retrieve the public_id from the URL. However, when you upload an image, the upload API call returns a response that includes the public_id.
    You can save the public_id and then get the URL and all the information by using the Admin API. Read here for more info:
    https://cloudinary.com/documentation/admin_api#get_the_details_of_a_single_resource

     

    1
    Comment actions Permalink
  • Avatar
    Peters Oluwafemi

    Okay does the public id include the folder names . 

    1
    Comment actions Permalink
  • Avatar
    Aleksandar Kostadinov

    Hi Peters,

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

    1
    Comment actions Permalink
  • Avatar
    Every Org

    Follow up to this question: given that we didn't store the images at the time of upload, is there a way to reliably get an image ID from a url when it may contain transforms? The above answers make it sound like the answer is "no" and we need to store the IDs alongside the raw URLs, but would be nice to confirm if there really is no way.

    For example, when we wanted to use a transform to combine two cloudinary urls into one with the other superimposed on it with the `l_{cloudinaryImageId}` tag in the transform, we needed to get an image IDs from two existing Cloudinary URLs, but the presence of folders made it unwieldy to do this in an way that feels safe. If there is a way, it would save us engineering time/db migration risk to know. Thanks!

    0
    Comment actions Permalink
  • Avatar
    Shirly Manor

    Hi,

    As you said, our best practice will be to have the public_id handy. and use it whenever you need.

    Currently, the way to find the public_id is only by string manipulation. That is not our recommended way. because there are some not standard ways to use our urls.

    However, if you are using Cloudinary without our SEO features. you can search for the second indexof `/` after upload. and until the last index of the `.`.

    A better way will be to get the public id with our [search API](https://cloudinary.com/documentation/search_api) and use them.

    Hope that helps,

    0
    Comment actions Permalink
  • Avatar
    dima

    I went with a regular expression as follows:

    $url = 'https://res.cloudinary.com/some-org/video/upload/v1594889564/some-video';

    preg_match("/upload\/(?:v\d+\/)?([^\.]+)/", $url, $matches);

    // $matches[1] contains "some-video"

    // works also for
    "https://res.cloudinary.com/some-org/image/upload/v1594889564/some-image.png" === "some-image"
    "https://res.cloudinary.com/some-org/raw/upload/v1594889564/some-thing.foobar" === "some-thing"
    "https://res.cloudinary.com/some-org/raw/upload/some-thing.foobar" === "some-thing"

    I did not implement transformations in the URLs, as we did not need it in our current project.

    1
    Comment actions Permalink
  • Avatar
    Aleksandar Kostadinov

    Hi Dima,

    That approach would work. However, if you can, I would recommend performing a one-off migration of the data you store in your database to store the individual parts of the asset's identifier (e.g. resource_type, type and public_id) rather than the whole URL. This way, it'll be a lot easier for you to use any of the API methods or local SDK methods to build URLs without needing RegExp.

    In addition, even if you don't need any custom transformations to change the visual aspects of the image, it's always best to include transformations that optimize the delivery of images rather than requesting originals which are most often large in size. For example, such as f_auto and q_auto transformations. Below I'm including two links to relevant support article/documentation with regards to image optimizations that provide more details -
    https://cloudinary.com/documentation/image_optimization
    http://support.cloudinary.com/hc/en-us/articles/202521522-How-can-I-make-my-images-load-faster-

    1
    Comment actions Permalink
  • Avatar
    David Ortega

    hey guys! in our case we are saving media files in Cloudinary through Strapi CMS, and when we get data from there we only receive the Cloudinary's URL, I hope you can find a way to get the public_Id from the url soon.

    0
    Comment actions Permalink
  • Avatar
    Aleksandar Kostadinov

    Hi David,

    I'm not familiar with Strapi myself, but I was wondering, how much control do you have (if any) in terms of the data you get back? When using the Cloudinary Media Library Widget to export/insert images from Cloudinary, we return a response similar to the of the API when you've uploaded a new image, so that includes the public_id also.

    In addition, could you share a few of the URLs you get back from the system - are they with the full version number? If so, that could allow you to use a simpler regular expression to extract the public_id. 

    0
    Comment actions Permalink

Please sign in to leave a comment.