1
.. _howto-custom-file-storage:
3
Writing a custom storage system
4
===============================
6
.. currentmodule:: django.core.files.storage
8
If you need to provide custom file storage -- a common example is storing files
9
on some remote system -- you can do so by defining a custom storage class.
10
You'll need to follow these steps:
12
#. Your custom storage system must be a subclass of
13
``django.core.files.storage.Storage``::
15
from django.core.files.storage import Storage
17
class MyStorage(Storage):
20
#. Django must be able to instantiate your storage system without any arguments.
21
This means that any settings should be taken from ``django.conf.settings``::
23
from django.conf import settings
24
from django.core.files.storage import Storage
26
class MyStorage(Storage):
27
def __init__(self, option=None):
29
option = settings.CUSTOM_STORAGE_OPTIONS
32
#. Your storage class must implement the ``_open()`` and ``_save()`` methods,
33
along with any other methods appropriate to your storage class. See below for
34
more on these methods.
36
In addition, if your class provides local file storage, it must override
37
the ``path()`` method.
39
Your custom storage system may override any of the storage methods explained in
40
:ref:`ref-files-storage`, but you **must** implement the following methods:
42
* :meth:`Storage.delete`
43
* :meth:`Storage.exists`
44
* :meth:`Storage.listdir`
45
* :meth:`Storage.size`
48
You'all also usually want to use hooks specifically designed for custom storage
51
``_open(name, mode='rb')``
52
~~~~~~~~~~~~~~~~~~~~~~~~~~
56
Called by ``Storage.open()``, this is the actual mechanism the storage class
57
uses to open the file. This must return a ``File`` object, though in most cases,
58
you'll want to return some subclass here that implements logic specific to the
59
backend storage system.
61
``_save(name, content)``
62
~~~~~~~~~~~~~~~~~~~~~~~~
64
Called by ``Storage.save()``. The ``name`` will already have gone through
65
``get_valid_name()`` and ``get_available_name()``, and the ``content`` will be a
66
``File`` object itself. No return value is expected.
68
``get_valid_name(name)``
69
------------------------
71
Returns a filename suitable for use with the underlying storage system. The
72
``name`` argument passed to this method is the original filename sent to the
73
server, after having any path information removed. Override this to customize
74
how non-standard characters are converted to safe filenames.
76
The code provided on ``Storage`` retains only alpha-numeric characters, periods
77
and underscores from the original filename, removing everything else.
79
``get_available_name(name)``
80
----------------------------
82
Returns a filename that is available in the storage mechanism, possibly taking
83
the provided filename into account. The ``name`` argument passed to this method
84
will have already cleaned to a filename valid for the storage system, according
85
to the ``get_valid_name()`` method described above.
87
The code provided on ``Storage`` simply appends underscores to the filename
88
until it finds one that's available in the destination directory.