Show S3 size in metrics script

pull/18/head
tycrek 4 years ago
parent 3bda2c0cb4
commit 24c05bfe69
No known key found for this signature in database
GPG Key ID: 25D74F3943625263

@ -1,6 +1,8 @@
const fs = require('fs-extra');
const path = require('path');
const { s3enabled } = require('./config.json');
const { formatBytes } = require('./utils');
const { bucketSize } = require('./storage');
module.exports = () => {
let data = fs.readJsonSync(path.join(__dirname, 'data.json'));
@ -21,9 +23,13 @@ module.exports = () => {
}
});
// Get AWS size
bucketSize()
.then((s3size) => {
console.log('---- Usage metrics ----\n');
console.log(`Users: ${Object.keys(users).length}`);
console.log(`Files: ${Object.keys(data).length}`);
console.log(`S3 size: ${s3enabled ? s3size : '--'}`);
console.log('');
console.log(`Total size: ${formatBytes(totalSize)}`);
console.log(`Untracked size: ${formatBytes(oldSize)}`);
@ -32,6 +38,8 @@ module.exports = () => {
Object.values(users).forEach(({ username, count, size }) => {
console.log(`- ${username}: ${formatBytes(size)} (${count} files)`);
});
})
.catch(console.error);
}
if (require.main === module) module.exports();

@ -6,7 +6,7 @@ const aws = require('aws-sdk');
const multer = require('multer');
const multerS3 = require('multer-s3');
const { getSafeExt } = require('./utils');
const { diskFilePath, saveWithDate, s3endpoint, s3bucket, s3accessKey, s3secretKey } = require('./config.json');
const { diskFilePath, saveWithDate, s3enabled, s3endpoint, s3bucket, s3accessKey, s3secretKey } = require('./config.json');
const s3 = new aws.S3({
endpoint: new aws.Endpoint(s3endpoint),
@ -44,4 +44,14 @@ const uploadLocal = multer({
})
}).single('file');
module.exports = { uploadLocal, uploadS3, deleteS3 };
const bucketSize = () =>
new Promise((resolve, reject) => s3enabled ? listAllKeys(resolve, reject) : resolve(0));
function listAllKeys(resolve, reject, token) {
let allKeys = [];
s3.listObjectsV2({ Bucket: s3bucket, ContinuationToken: token }).promise()
.then((data) => (allKeys = allKeys.concat(data.Contents), data.IsTruncated ? listAllKeys(resolve, reject, data.NextContinuationToken) : resolve(allKeys.length)))
.catch(reject);
}
module.exports = { uploadLocal, uploadS3, deleteS3, bucketSize };

Loading…
Cancel
Save