Created
August 24, 2016 09:29
-
-
Save SindreSvendby/96d98860e2c2c399dc8f53cf55fbe67a to your computer and use it in GitHub Desktop.
Report the size of a folder in cloudinary (below upload)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// npm install cloudinary | |
const cloudinary = require('cloudinary'); | |
const folder_in_upload = process.env.FOLDER | |
cloudinary.config({ | |
cloud_name: process.env.CLOUD_NAME, | |
api_key: process.env.API_KEY, | |
api_secret: process.env.API_SECRET | |
}); | |
cloudinary.api.resources(result => { | |
const bytes = getBytesOfAllPictures(result.resources) | |
retrivePage(result.resources.next_cursor, bytes) | |
.then(totaleBytes => console.log(`bytes is: ${totaleBytes}`)) | |
}) | |
page = 0 | |
function retrivePage(next_cursor_id, aggBytes) { | |
return new Promise((resolve, reject) => { | |
cloudinary.api.resources(result => { | |
currentBytes = getBytesOfAllPictures(result.resources) + aggBytes; | |
const { next_cursor, rate_limit_remaining } = result; | |
if(rate_limit_remaining > 0 && next_cursor) { | |
console.log(`Still ${rate_limit_remaining} requests left`) | |
console.log(`Cursor is ${next_cursor}`) | |
console.log(`currentBytes is ${currentBytes}`); | |
console.log(`KB is ${currentBytes / 1024 }`); | |
console.log(`MB is ${currentBytes / 1024 / 1024 }`); | |
console.log(`GB is ${currentBytes / 1024 / 1024 / 1024}`); | |
console.log(`Processed images is ${++page * 500}`); | |
resolve(retrivePage(next_cursor, currentBytes)) | |
} else { | |
console.log('Existing') | |
console.log('rate_limit_remaining:', rate_limit_remaining) | |
console.log('next_cursor:', next_cursor) | |
//if no cursor or rate_limit_remaining is 0, return bytes | |
resolve(currentBytes) | |
} | |
}, {next_cursor: next_cursor_id, max_results: 500, type: 'upload', prefix: folder_in_upload}) | |
}) | |
} | |
function getBytesOfAllPictures(pictures) { | |
return pictures.reduce((size, picture) => { | |
return size + picture.bytes | |
}, 0) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment