Skip to content

Commit

Permalink
#99 show media sizes of production storage backend instead of hard co…
Browse files Browse the repository at this point in the history
…ded s3
  • Loading branch information
ephes committed Aug 14, 2023
1 parent 70482e5 commit 04f5cc2
Showing 1 changed file with 15 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
from django.core.files.storage import get_storage_class
from django.core.management.base import BaseCommand

from cast.utils import storage_walk_paths

from .storage_backend import get_production_and_backup_storage


class Command(BaseCommand):
help = "shows size of media on s3"
help = (
"show size of media files on production storage backend"
"(requires Django >= 4.2 and production and backup storage configured)"
)

@staticmethod
def show_usage(paths):
Expand All @@ -26,11 +30,16 @@ def show_usage(paths):
print(f"misc usage: {misc / unit}")
print(f"total usage: {sum(paths.values()) / unit}")

def handle(self, *args, **options):
s3 = get_storage_class("storages.backends.s3boto3.S3Boto3Storage")()
@staticmethod
def get_paths_with_sizes_for(storage_backend):
paths = {}
for path in storage_walk_paths(s3):
size = s3.size(path)
for path in storage_walk_paths(storage_backend):
size = storage_backend.size(path)
paths[path] = size
print(path, size / 2**20)
return paths

def handle(self, *args, **options):
production, _ = get_production_and_backup_storage()
paths = self.get_paths_with_sizes_for(production)
self.show_usage(paths)

0 comments on commit 04f5cc2

Please sign in to comment.