custom-file-storage.txt 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. .. _howto-custom-file-storage:
  2. Writing a custom storage system
  3. ===============================
  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 ``_open()`` and ``_save()`` methods,
  22. along with any other methods appropriate to your storage class. See below for
  23. more on these methods.
  24. In addition, if your class provides local file storage, it must override
  25. the ``path()`` method.
  26. Your custom storage system may override any of the storage methods explained in
  27. :ref:`ref-files-storage`. However, it's usually better to use the hooks
  28. specifically designed for custom storage objects. These are:
  29. ``_open(name, mode='rb')``
  30. ~~~~~~~~~~~~~~~~~~~~~~~~~~
  31. **Required**.
  32. Called by ``Storage.open()``, this is the actual mechanism the storage class
  33. uses to open the file. This must return a ``File`` object, though in most cases,
  34. you'll want to return some subclass here that implements logic specific to the
  35. backend storage system.
  36. ``_save(name, content)``
  37. ~~~~~~~~~~~~~~~~~~~~~~~~
  38. Called by ``Storage.save()``. The ``name`` will already have gone through
  39. ``get_valid_name()`` and ``get_available_name()``, and the ``content`` will be a
  40. ``File`` object itself. No return value is expected.
  41. ``get_valid_name(name)``
  42. ------------------------
  43. Returns a filename suitable for use with the underlying storage system. The
  44. ``name`` argument passed to this method is the original filename sent to the
  45. server, after having any path information removed. Override this to customize
  46. how non-standard characters are converted to safe filenames.
  47. The code provided on ``Storage`` retains only alpha-numeric characters, periods
  48. and underscores from the original filename, removing everything else.
  49. ``get_available_name(name)``
  50. ----------------------------
  51. Returns a filename that is available in the storage mechanism, possibly taking
  52. the provided filename into account. The ``name`` argument passed to this method
  53. will have already cleaned to a filename valid for the storage system, according
  54. to the ``get_valid_name()`` method described above.
  55. The code provided on ``Storage`` simply appends underscores to the filename
  56. until it finds one that's available in the destination directory.