Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 'use strict';
- const Promise = require('bluebird');
- const filesize = require('filesize');
- const AWS = require('aws-sdk');
- // AWS.config.update({
- // accessKeyId: '',
- // secretAccessKey: '',
- // region: ''
- // });
- const s3 = new AWS.S3();
- const cloudwatch = new AWS.CloudWatch();
- const reducer = (acc, currVal) => acc + currVal;
- async function go() {
- try {
- const data = await s3.listBuckets().promise();
- const names = data.Buckets.map(bucket => bucket.Name);
- const sizes = await Promise.mapSeries(names, async name => {
- try {
- const opts = {
- StartTime: new Date('2019-06-03T00:00:00'),
- EndTime: new Date('2019-06-13T00:00:00'),
- MetricName: 'BucketSizeBytes',
- Namespace: 'AWS/S3',
- Period: 86400 / 2,
- Dimensions: [{ Name: 'BucketName', Value: name }, { Name: 'StorageType', Value: 'StandardStorage' }],
- Statistics: ['Maximum']
- };
- const stats = await cloudwatch.getMetricStatistics(opts).promise();
- const points = stats.Datapoints;
- if (typeof points.pop() === 'undefined') {
- console.log(`${name} - ${filesize(0)}`);
- return 0;
- } else {
- console.log(`${name} - ${filesize(points.pop().Maximum)}`);
- return points.pop().Maximum;
- }
- } catch (e) {
- console.log(e);
- }
- });
- console.log(filesize(sizes.reduce(reducer)));
- } catch (e) {
- console.log(e);
- }
- }
- go(); // run
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement