find single assest using public_id

kkumar_gcc
kkumar_gcc Member Posts: 3
edited August 2023 in Developer APIs

I want to retrieve an asset using its public_id.

Here's the code I'm currently using:

r.instance.Upload.Explicit(r.ctx, uploader.ExplicitParams{
    PublicID:     r.getPublicId(path),
    Type:         "upload",
    ResourceType: r.getResourceType(path),
})

The issue I'm facing is determining the appropriate ResourceType to use. Is there any alternative way which can be used to retrieve asset without using ResourceType ?(If i don't use ResourceType , it defaults to image , which is not what I want.)

Answers

  • Vdeub
    Vdeub Member, Cloudinary Staff Posts: 78

    Hi @kkumar_gcc,

    If you don't know the type and the resource_type , the best option is to actually use the Search API and search for the public_id . From there, you will get the resource_type and the type.

    Have in mind that the Search API is rate-limited so it shouldn't be used like a Search engine. If this is a requirement, when you upload an asset, you will get all the information related to the asset so you should store those in your database.

    Hope that helps.

    Best,

    Loic

  • kkumar_gcc
    kkumar_gcc Member Posts: 3
    edited August 2023

    Thanks for quick reply @Vdeub,

    Okay, i will use SearchApi then.

    If i want to delete a resource by PublicID other than image resource_type, i have to mention it. Can't it be done without using resource_type? (is there any alternative of it?)

    result, err := r.instance.Upload.Destroy(r.ctx, uploader.DestroyParams{
        PublicID:   r.getPublicId(f),
        Invalidate: api.Bool(true),
    })
    // if i want to delete any other `type` of resource i have to add ResourceType
    
  • Vdeub
    Vdeub Member, Cloudinary Staff Posts: 78

    @kkumar_gcc no it is not slower, it is just a different API that is rate-limited.

    If you want to delete an asset without specifying the resource_type and the type, you can't use the Upload API. You can do so using the Admin API `delete_resources` method passing the all parameter as documented here.

    Loic