django/django · error · NotImplementedError

subclasses of Storage must provide a size() method

Error message

subclasses of Storage must provide a size() method

What it means

Raised as NotImplementedError by the base Storage.size() abstract method. Subclasses must implement size(self, name) returning the byte size of the stored file. The base raises at storage/base.py:172.

Source

Thrown at django/core/files/storage/base.py:172

        """
        raise NotImplementedError(
            "subclasses of Storage must provide an exists() method"
        )

    def listdir(self, path):
        """
        List the contents of the specified path. Return a 2-tuple of lists:
        the first item being directories, the second item being files.
        """
        raise NotImplementedError(
            "subclasses of Storage must provide a listdir() method"
        )

    def size(self, name):
        """
        Return the total size, in bytes, of the file specified by name.
        """
        raise NotImplementedError("subclasses of Storage must provide a size() method")

    def url(self, name):
        """
        Return an absolute URL where the file's contents can be accessed
        directly by a web browser.
        """
        raise NotImplementedError("subclasses of Storage must provide a url() method")

    def get_accessed_time(self, name):
        """
        Return the last accessed time (as a datetime) of the file specified by
        name. The datetime will be timezone-aware if USE_TZ=True.
        """
        raise NotImplementedError(
            "subclasses of Storage must provide a get_accessed_time() method"
        )

    def get_created_time(self, name):

View on GitHub (pinned to b5388a3a80)

Solutions

  1. Implement size(self, name) in the custom Storage subclass, e.g. via a HEAD request returning Content-Length.
  2. Subclass FileSystemStorage or a complete third-party backend.
  3. Cache size metadata to avoid repeated backend calls.
  4. Guard callers with getattr/try-except if size is best-effort.

Example fix

# before
class MyStorage(Storage):
    ...
# after
class MyStorage(Storage):
    def size(self, name):
        return self.client.head_object(Key=name).get('ContentLength', 0)
Defensive patterns

Strategy: type-guard

Validate before calling

def storage_can_size(storage) -> bool:
    return 'size' in storage.__class__.__dict__

Type guard

def implements_size(storage) -> bool:
    return any('size' in c.__dict__ for c in storage.__class__.__mro__)

Try / catch

try:
    sz = storage.size(name)
except NotImplementedError:
    sz = None  # or compute via storage.open(name) and len()

Prevention

When it happens

Trigger: Calling storage.size(name) on a Storage subclass that did not override size(). Hit during file-size display in admin, validation of upload size limits, or any code calling .size() on a custom backend.

Common situations: Custom object-storage backend missing size(); admin templates that display file sizes triggering the abstract method; ImageField or validators that check size() before processing.

Related errors


AI-assisted analysis of django/django@b5388a3a80 (2026-08-10). Data as JSON: /api/errors/2330126684526087. Report an issue: GitHub.