Use the Cloudinary Search API to retrieve assets from a specific folder and sort them by their `created_at` timestamp. This will allow you to identify and filter out X number of assets by date. The API returns a maximum of 500 assets per call, if you have more assets, you'll need to iterate over the results using the `next_cursor`.
Below is an example in PHP:
$result = $cloudinary->searchApi()
->expression('folder:"<FOLDER_NAME>"')
->sortBy('created_at','desc')
->maxResults(500)
->nextCursor('xxx') --> iterate here with thenext_cursor
from the previous
$result ->execute();
Here’s the same example using Node.js, let's say the maximum number of results we need is 200:
const cloudinary = require('cloudinary').v2;
async function getOldestAssets(folderName, maxResults = 200) {
let assets = [];
let nextCursor = null;
do {
let result = await cloudinary.search
.expression(`folder:"${folderName}"`)
.sort_by('created_at', 'desc') // Sort in descending order to get the newest first
.max_results(500)
.next_cursor(nextCursor)
.execute();
assets = assets.concat(result.resources);
nextCursor = result.next_cursor;
} while (nextCursor && assets.length < maxResults);
// Return the 200 oldest assets if there are at least 200
return assets.slice(-maxResults);
}
// Usage example
getOldestAssets('folder_name')
.then(oldestAssets => { console.log('Oldest assets:', oldestAssets); })
.catch(error => { console.error('Error fetching assets:', error); });
Comments
0 comments
Please sign in to leave a comment.