How do i fetch a resource by it's public id when it is in a folder

Options
simonwilbert
simonwilbert Member Posts: 5 ✭
edited September 2023 in Developer APIs
import { v2 as cloudinary } from "cloudinary";

const public_id = 'samples/sea-turtle';

const asset1 = await cloudinary.api.resource(public_id);
const asset2 = await cloudinary.api.resource('s:' + encodeURI(public_id));
const asset3 = await cloudinary.api.resource('s64:' + btoa(public_id));

neither of the above resource calls will fetch a resource within the 'samples' folder. As you see I've tried many variations using URI encoding and prefixing with s: or base64 encoding using the s64: prefix

what did work for me was

const assets = await cloudinary.search
  .expression(`public_id=samples/sea-turtle`)
  .execute();

but search naturally pulls an array back, not an exact match for 1 resource

Any advice would be great! I was using the following documentation page for reference https://cloudinary.com/documentation/admin_api#get_details_of_a_single_resource_by_public_id

Tagged:

Answers

  • Cloudinary Team
    Cloudinary Team Administrator, Cloudinary Staff Posts: 123 admin
    Options

    Hi there,

    Thanks for reaching out.

    I see that you are trying to grab the details of a video. That means that you will need to pass the resource_type parameter to the method call. If no resource type is passed, it will assume you are searching for an image. There is no image in your account with a public id of samples/sea-turtle. That's why you are getting the 'resource not found' error returned.

    cloudinary.api
        .resource("samples/sea-turtle", {
            resource_type: "video",
        }).then(res => console.log(res)).catch(err => console.log(err))
    

    You can find this mentioned in the documentation here:
    https://cloudinary.com/documentation/admin_api#get_details_of_a_single_resource_by_public_id

    I hope this helps. Let me know if you have any questions.

    Kind regards,

    Tia

    Helpful Links For You
    πŸ’¬ Share questions, connect with other users in our Cloudinary Community forums and Discord server!
    πŸ§‘β€πŸŽ“ Join our Cloudinary Academy for free courses, workshops and other educational resources.
    πŸ“„ Read our documentation for in-depth details on Cloudinary product features and capabilities
    πŸ“° Check out the Cloudinary blog for the latest company news and insights

  • simonwilbert
    simonwilbert Member Posts: 5 ✭
    Options

    Thankyou. perfect!

  • Cloudinary Team
    Cloudinary Team Administrator, Cloudinary Staff Posts: 123 admin
    Options

    Hi there,

    My pleasure 😊 Glad it's working as expected now.

    Kind regards,

    Tia

    Helpful Links For You
    πŸ’¬ Share questions, connect with other users in our Cloudinary Community forums and Discord server!
    πŸ§‘β€πŸŽ“ Join our Cloudinary Academy for free courses, workshops and other educational resources.
    πŸ“„ Read our documentation for in-depth details on Cloudinary product features and capabilities
    πŸ“° Check out the Cloudinary blog for the latest company news and insights