How to use 'next_cursor' to get the rest of the files in a specific folder.
I am using the api to get a json list of the files in a specific folder. Here is an example of what I have entered:
require('dotenv').config();
const cloudinary = require('cloudinary').v2;
cloudinary.api.resources({
resource_type: 'image',
type: 'upload',
prefix: 'folder_name/subfolder_name'
})
.then(result => console.log(result))
.catch(error => console.error(error));
It returns a json list but it only has part of the files listed and I see this at the end of the listing:
next_cursor: '00de06963ec3209c24ffceb733a884958711f1c5bf7f5d62f432f2f462f9f16cedff95bd483bf79e4fd364f6409fb650a9e46b273e88eba0338891a52ff9cbc3',
What am I supposed to do with this to get the rest of the files in the list? I'm sure this has something to do with getting the rest of the list, I just don't know how to use it in the code above. I'm a noob so please be specific about what the code should look like. Thx.
-
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);2
Post is closed for comments.
Comments
1 comment