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
- Implement size(self, name) in the custom Storage subclass, e.g. via a HEAD request returning Content-Length.
- Subclass FileSystemStorage or a complete third-party backend.
- Cache size metadata to avoid repeated backend calls.
- 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
- Implement size(self, name) -> int in custom backends.
- Guard admin/validator code that calls .size() with try/except.
- Subclass a complete backend like FileSystemStorage.
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
- subclasses of Storage must provide a delete() method
- subclasses of Storage must provide an exists() method
- subclasses of Storage must provide a listdir() method
- subclasses of Storage must provide a url() method
- subclasses of SessionBase must provide an exists() method
AI-assisted analysis of django/django@b5388a3a80 (2026-08-10).
Data as JSON: /api/errors/2330126684526087.
Report an issue: GitHub.