custom-file-storage.txt 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. Writing a custom storage system
  2. ===============================
  3. .. currentmodule:: django.core.files.storage
  4. If you need to provide custom file storage -- a common example is storing files
  5. on some remote system -- you can do so by defining a custom storage class.
  6. You'll need to follow these steps:
  7. #. Your custom storage system must be a subclass of
  8. ``django.core.files.storage.Storage``::
  9. from django.core.files.storage import Storage
  10. class MyStorage(Storage):
  11. ...
  12. #. Django must be able to instantiate your storage system without any arguments.
  13. This means that any settings should be taken from ``django.conf.settings``::
  14. from django.conf import settings
  15. from django.core.files.storage import Storage
  16. class MyStorage(Storage):
  17. def __init__(self, option=None):
  18. if not option:
  19. option = settings.CUSTOM_STORAGE_OPTIONS
  20. ...
  21. #. Your storage class must implement the :meth:`_open()` and :meth:`_save()`
  22. methods, along with any other methods appropriate to your storage class. See
  23. below for more on these methods.
  24. In addition, if your class provides local file storage, it must override
  25. the ``path()`` method.
  26. #. Your storage class must be :ref:`deconstructible <custom-deconstruct-method>`
  27. so it can be serialized when it's used on a field in a migration. As long
  28. as your field has arguments that are themselves
  29. :ref:`serializable <migration-serializing>`, you can use the
  30. ``django.utils.deconstruct.deconstructible`` class decorator for this
  31. (that's what Django uses on FileSystemStorage).
  32. Your custom storage system may override any of the storage methods explained in
  33. :doc:`/ref/files/storage`, but you **must** implement the following methods:
  34. * :meth:`Storage.delete`
  35. * :meth:`Storage.exists`
  36. * :meth:`Storage.listdir`
  37. * :meth:`Storage.size`
  38. * :meth:`Storage.url`
  39. You'll also usually want to use hooks specifically designed for custom storage
  40. objects. These are:
  41. .. method:: _open(name, mode='rb')
  42. **Required**.
  43. Called by ``Storage.open()``, this is the actual mechanism the storage class
  44. uses to open the file. This must return a ``File`` object, though in most cases,
  45. you'll want to return some subclass here that implements logic specific to the
  46. backend storage system.
  47. .. method:: _save(name, content)
  48. Called by ``Storage.save()``. The ``name`` will already have gone through
  49. ``get_valid_name()`` and ``get_available_name()``, and the ``content`` will be a
  50. ``File`` object itself.
  51. Should return the actual name of name of the file saved (usually the ``name``
  52. passed in, but if the storage needs to change the file name return the new name
  53. instead).
  54. .. method:: get_valid_name(name)
  55. Returns a filename suitable for use with the underlying storage system. The
  56. ``name`` argument passed to this method is the original filename sent to the
  57. server, after having any path information removed. Override this to customize
  58. how non-standard characters are converted to safe filenames.
  59. The code provided on ``Storage`` retains only alpha-numeric characters, periods
  60. and underscores from the original filename, removing everything else.
  61. .. method:: get_available_name(name)
  62. Returns a filename that is available in the storage mechanism, possibly taking
  63. the provided filename into account. The ``name`` argument passed to this method
  64. will have already cleaned to a filename valid for the storage system, according
  65. to the ``get_valid_name()`` method described above.
  66. The code provided on ``Storage`` simply appends ``"_1"``, ``"_2"``, etc. to the
  67. filename until it finds one that's available in the destination directory.