SHARE
TWEET

Untitled

a guest Oct 22nd, 2019 91 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 'use strict';
  2.  
  3. const Promise = require('bluebird');
  4. const filesize = require('filesize');
  5.  
  6. const AWS = require('aws-sdk');
  7. // AWS.config.update({
  8. //   accessKeyId: '',
  9. //   secretAccessKey: '',
  10. //   region: ''
  11. // });
  12. const s3 = new AWS.S3();
  13. const cloudwatch = new AWS.CloudWatch();
  14.  
  15. const reducer = (acc, currVal) => acc + currVal;
  16.  
  17. async function go() {
  18.   try {
  19.     const data = await s3.listBuckets().promise();
  20.     const names = data.Buckets.map(bucket => bucket.Name);
  21.     const sizes = await Promise.mapSeries(names, async name => {
  22.       try {
  23.         const opts = {
  24.           StartTime: new Date('2019-06-03T00:00:00'),
  25.           EndTime: new Date('2019-06-13T00:00:00'),
  26.           MetricName: 'BucketSizeBytes',
  27.           Namespace: 'AWS/S3',
  28.           Period: 86400 / 2,
  29.           Dimensions: [{ Name: 'BucketName', Value: name }, { Name: 'StorageType', Value: 'StandardStorage' }],
  30.           Statistics: ['Maximum']
  31.         };
  32.         const stats = await cloudwatch.getMetricStatistics(opts).promise();
  33.         const points = stats.Datapoints;
  34.         if (typeof points.pop() === 'undefined') {
  35.           console.log(`${name} - ${filesize(0)}`);
  36.           return 0;
  37.         } else {
  38.           console.log(`${name} - ${filesize(points.pop().Maximum)}`);
  39.           return points.pop().Maximum;
  40.         }
  41.       } catch (e) {
  42.         console.log(e);
  43.       }
  44.     });
  45.     console.log(filesize(sizes.reduce(reducer)));
  46.   } catch (e) {
  47.     console.log(e);
  48.   }
  49. }
  50.  
  51. go(); // run
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top