How to use 'next_cursor' to get the rest of the files in a specific folder.

Comments

1 comment

  • Avatar
    Aleksandar Kostadinov

    Hi Jackie,

    You're correct in assuming you'll need to use the returned next_cursor. It's used to manage the Pagination process. A single API call can contain the max_results parameter. When not specified different API calls have different max_results default values. These also have maximum value limits. Any query or API call that matches more resources than max_results will return a next_cursor that can be used to get the next set of results.

    When next_cursor is returned in a response, you'll need to grab that value and set it in the subsequent API call to get the next max_results number of results. For example, the get_resources API method you're using has a max_results default value of 10 and maximum is 500.

    For example, below recursively grabs the secure_urls of all resources based on the specified criteria. Firstly it defaults to a Null next_cursor and if the first API call returns one we call the same function again but passing next_cursor as a parameter. That process repeats recursively until there is no next_cursor and we've paginated through all results.

    function list_resources(results, next_cursor = null) {

        cloudinary.api.resources(
            {
                resource_type: "image",
                type: "upload",
                prefix: "some/folder",
    mex_results: 100, //can be any value up to 500
                next_cursor: next_cursor
            },
            function(err, res) {

            res.resources.forEach(function(resource){
    //Do some processing or checks
                results.push(resource.secure_url);
            });

            if (res.next_cursor) {
            list_resources(results, res.next_cursor);
            } else {
    console.log("Done", results);
    }
        });
    }

    let results = [];
    list_resources(results);
    0
    Comment actions Permalink

Please sign in to leave a comment.