custom-file-storage.txt 3.2 KB

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