def get(self): parser = init_parser().copy() parser.add_argument("bucket_name", type=str) args = parser.parse_args() secret_key = args["secret_key"].replace(" ", "+") try: if args["bucket_name"]: total_size, total_objects = bucket.bucket_usage( get_resources(args["access_key"], secret_key), args["bucket_name"]) bucket_usage = { "name": args["bucket_name"], "size": total_size, "objects": total_objects, } return response(200, data=bucket_usage) disk_usage = {"bucket": [], "total_usage": 0} disk_usages = bucket.disk_usage( get_resources(args["access_key"], secret_key)) for usage in disk_usages: bucket_name = usage[0] total_size, total_objects = usage[1] disk_usage["total_usage"] += total_size disk_usage["bucket"].append({ "name": bucket_name, "size": total_size, "objects": total_objects }) disk_usage["total_usage"] = f"{disk_usage['total_usage']}" return response(200, data=disk_usage) except Exception as e: current_app.logger.error(f"{e}", exc_info=args["debug"]) return response(500, f"{e}")
def test_disk_usage(self, monkeypatch): monkeypatch.setattr(bucket, "buckets", self.fake_bucket) monkeypatch.setattr(bucket, "bucket_usage", self.fake_usage) assert bucket.disk_usage("boom") == [ ["bucket0", (819, 3)], ["bucket1", (1092, 4)], ["bucket2", (1365, 5)], ]
def disk_usage(resource): try: disk_usages = bucket_lib.disk_usage(resource) total_usage = 0 for usage in disk_usages: bucket_name = usage[0] total_size, total_objects = usage[1] human_total_size = bitmath.Byte(total_size).best_prefix() total_usage += total_size click.secho( f'{human_total_size.format("{value:.2f} {unit}")}, {total_objects} objects in "{bucket_name}" bucket' ) human_total_usage = bitmath.Byte(total_usage).best_prefix() click.secho(f"---\n" f"{human_total_usage.format('{value:.2f} {unit}')} Total") except Exception as exc: click.secho( f"Disk usage fetching failed. \n{exc}", fg="yellow", bold=True, err=True )