PageRenderTime 39ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

Plain Text | 90 lines | 65 code | 25 blank | 0 comment | 0 complexity | 4b50ab217ce0d01559bdaec25a082003 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. Writing a custom storage system
  2. ===============================
  3. .. currentmodule::
  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. ````::
  9. from 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 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. :doc:`/ref/files/storage`, but you **must** implement the following methods:
  28. * :meth:`Storage.delete`
  29. * :meth:`Storage.exists`
  30. * :meth:`Storage.listdir`
  31. * :meth:`Storage.size`
  32. * :meth:`Storage.url`
  33. You'll also usually want to use hooks specifically designed for custom storage
  34. objects. These are:
  35. ``_open(name, mode='rb')``
  36. ~~~~~~~~~~~~~~~~~~~~~~~~~~
  37. **Required**.
  38. Called by ````, this is the actual mechanism the storage class
  39. uses to open the file. This must return a ``File`` object, though in most cases,
  40. you'll want to return some subclass here that implements logic specific to the
  41. backend storage system.
  42. ``_save(name, content)``
  43. ~~~~~~~~~~~~~~~~~~~~~~~~
  44. Called by ````. The ``name`` will already have gone through
  45. ``get_valid_name()`` and ``get_available_name()``, and the ``content`` will be a
  46. ``File`` object itself.
  47. Should return the actual name of name of the file saved (usually the ``name``
  48. passed in, but if the storage needs to change the file name return the new name
  49. instead).
  50. ``get_valid_name(name)``
  51. ~~~~~~~~~~~~~~~~~~~~~~~~
  52. Returns a filename suitable for use with the underlying storage system. The
  53. ``name`` argument passed to this method is the original filename sent to the
  54. server, after having any path information removed. Override this to customize
  55. how non-standard characters are converted to safe filenames.
  56. The code provided on ``Storage`` retains only alpha-numeric characters, periods
  57. and underscores from the original filename, removing everything else.
  58. ``get_available_name(name)``
  59. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  60. Returns a filename that is available in the storage mechanism, possibly taking
  61. the provided filename into account. The ``name`` argument passed to this method
  62. will have already cleaned to a filename valid for the storage system, according
  63. to the ``get_valid_name()`` method described above.
  64. The code provided on ``Storage`` simply appends ``"_1"``, ``"_2"``, etc. to the
  65. filename until it finds one that's available in the destination directory.