Skip to content

Instantly share code, notes, and snippets.

@woutersf
Last active October 5, 2023 10:44
Show Gist options
  • Save woutersf/06fa8a27a9e0ccdd4f13f8e7e971f3aa to your computer and use it in GitHub Desktop.
Save woutersf/06fa8a27a9e0ccdd4f13f8e7e971f3aa to your computer and use it in GitHub Desktop.
Cloudinary export Metadata from FOLDER
/**
1. Make sure the exported file does not exist (archive.csv)
2. Replace the following with the correct information:
- THE_FOLDER_ON_THE_DAM
- MYCLOUDNAME_HERE
- MY_API_KEY_HERE
- MY_API_SECRET_HERE
3. npm install csv-writer and npm install cloudinary
4. Run with 'node cloudinary_metadata_export.js'
*/
var cloudinary = require('cloudinary').v2;
var exec = require('child_process').exec;
var createCsvWriter = require('csv-writer').createArrayCsvWriter;
var csvWriter = createCsvWriter({
header: ['public_id','url','tags','metadata'],
path: './archive.csv'
});
cloudinary.config({
cloud_name: 'MYCLOUDNAME_HERE',
api_key: 'MY_API_KEY_HERE',
api_secret: 'MY_API_SECRET_HERE',
secure:true
});
var result = [];
function listResources(next_cursor) {
var nextCursor = next_cursor;
cloudinary.search
.expression('status="active" AND folder="THE_FOLDER_ON_THE_DAM/*"')
.sort_by('uploaded_at','desc')
.with_field('metadata')
.with_field('tags')
.with_field('image_analysis')
.max_results(500)
.next_cursor(nextCursor)
.execute()
.then(res => {
var more = res.next_cursor;
resources = res.resources;
for(let res of resources)
{
var resultTemp = [];
var publicId = res.public_id;
var url = res.url;
var tags = res.tags;
var metadata = res.metadata;
resultTemp.push(publicId);
resultTemp.push(url);
resultTemp.push(tags.toString());
resultTemp.push(JSON.stringify(metadata));
result.push(resultTemp);
}
console.log(more);
if (more) {
listResources(more);
}
else {
console.log("done");
var resultArray = result;
csvWriter.writeRecords(resultArray).then(() => {
var size = resultArray.length;
console.log(size + ' Items saved to archive.csv!');
}).catch(e => {
console.error('Error occurred!', e.stack);
});
return;
}
});
}
listResources(null);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment