-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstorage.js
More file actions
61 lines (49 loc) · 1.83 KB
/
Copy pathstorage.js
File metadata and controls
61 lines (49 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/env node
/**
* storage.js
* Shows R2 bucket storage usage broken down by prefix,
* compared against the Cloudflare free tier limits.
*
* Usage: node storage.js
*/
'use strict';
const r2 = require('./r2');
const FREE_TIER_BYTES = 10 * 1024 ** 3; // 10 GB
function fmt(bytes) {
if (bytes >= 1024 ** 3) return (bytes / 1024 ** 3).toFixed(2) + ' GB';
if (bytes >= 1024 ** 2) return (bytes / 1024 ** 2).toFixed(1) + ' MB';
if (bytes >= 1024) return (bytes / 1024).toFixed(1) + ' KB';
return bytes + ' B';
}
function bar(used, total, width = 30) {
const filled = Math.round((used / total) * width);
return '[' + '█'.repeat(filled) + '░'.repeat(width - filled) + ']';
}
(async () => {
console.log(`\nBucket: ${r2.BUCKET_NAME}`);
process.stdout.write('Listing all objects… ');
const all = await r2.listAll();
console.log(`${all.length} objects\n`);
// Group by top-level prefix
const groups = {};
let total = 0;
for (const { key, size } of all) {
const prefix = key.split('/')[0];
groups[prefix] = groups[prefix] || { count: 0, size: 0 };
groups[prefix].count++;
groups[prefix].size += size;
total += size;
}
// Print breakdown
console.log('Storage by prefix:\n');
for (const [prefix, { count, size }] of Object.entries(groups).sort((a, b) => b[1].size - a[1].size)) {
console.log(` ${prefix.padEnd(14)} ${fmt(size).padStart(10)} (${count} files)`);
}
console.log('\n' + '─'.repeat(50));
console.log(` ${'Total'.padEnd(14)} ${fmt(total).padStart(10)} (${all.length} files)`);
console.log('');
const pct = ((total / FREE_TIER_BYTES) * 100).toFixed(1);
console.log(` Free tier usage: ${pct}% of 10 GB`);
console.log(` ${bar(total, FREE_TIER_BYTES)} ${fmt(total)} / ${fmt(FREE_TIER_BYTES)}`);
console.log(` Remaining: ${fmt(FREE_TIER_BYTES - total)}\n`);
})();