storage.txt 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. ================
  2. File storage API
  3. ================
  4. .. module:: django.core.files.storage
  5. Getting the default storage class
  6. =================================
  7. Django provides convenient ways to access the default storage class:
  8. .. data:: storages
  9. Storage instances as defined by :setting:`STORAGES`.
  10. .. class:: DefaultStorage
  11. :class:`~django.core.files.storage.DefaultStorage` provides
  12. lazy access to the default storage system as defined by ``default`` key in
  13. :setting:`STORAGES`. :class:`DefaultStorage` uses
  14. :data:`~django.core.files.storage.storages` internally.
  15. .. data:: default_storage
  16. :data:`~django.core.files.storage.default_storage` is an instance of the
  17. :class:`~django.core.files.storage.DefaultStorage`.
  18. The ``FileSystemStorage`` class
  19. ===============================
  20. .. class:: FileSystemStorage(location=None, base_url=None, file_permissions_mode=None, directory_permissions_mode=None, allow_overwrite=False)
  21. The :class:`~django.core.files.storage.FileSystemStorage` class implements
  22. basic file storage on a local filesystem. It inherits from
  23. :class:`~django.core.files.storage.Storage` and provides implementations
  24. for all the public methods thereof.
  25. .. note::
  26. The ``FileSystemStorage.delete()`` method will not raise an exception
  27. if the given file name does not exist.
  28. .. attribute:: location
  29. Absolute path to the directory that will hold the files.
  30. Defaults to the value of your :setting:`MEDIA_ROOT` setting.
  31. .. attribute:: base_url
  32. URL that serves the files stored at this location.
  33. Defaults to the value of your :setting:`MEDIA_URL` setting.
  34. .. attribute:: file_permissions_mode
  35. The file system permissions that the file will receive when it is
  36. saved. Defaults to :setting:`FILE_UPLOAD_PERMISSIONS`.
  37. .. attribute:: directory_permissions_mode
  38. The file system permissions that the directory will receive when it is
  39. saved. Defaults to :setting:`FILE_UPLOAD_DIRECTORY_PERMISSIONS`.
  40. .. attribute:: allow_overwrite
  41. .. versionadded:: 5.1
  42. Flag to control allowing saving a new file over an existing one.
  43. Defaults to ``False``.
  44. .. method:: get_created_time(name)
  45. Returns a :class:`~datetime.datetime` of the system's ctime, i.e.
  46. :func:`os.path.getctime`. On some systems (like Unix), this is the
  47. time of the last metadata change, and on others (like Windows), it's
  48. the creation time of the file.
  49. The ``InMemoryStorage`` class
  50. =============================
  51. .. class:: InMemoryStorage(location=None, base_url=None, file_permissions_mode=None, directory_permissions_mode=None)
  52. The :class:`~django.core.files.storage.InMemoryStorage` class implements
  53. a memory-based file storage. It has no persistence, but can be useful for
  54. speeding up tests by avoiding disk access.
  55. .. attribute:: location
  56. Absolute path to the directory name assigned to files. Defaults to the
  57. value of your :setting:`MEDIA_ROOT` setting.
  58. .. attribute:: base_url
  59. URL that serves the files stored at this location.
  60. Defaults to the value of your :setting:`MEDIA_URL` setting.
  61. .. attribute:: file_permissions_mode
  62. The file system permissions assigned to files, provided for
  63. compatibility with ``FileSystemStorage``. Defaults to
  64. :setting:`FILE_UPLOAD_PERMISSIONS`.
  65. .. attribute:: directory_permissions_mode
  66. The file system permissions assigned to directories, provided for
  67. compatibility with ``FileSystemStorage``. Defaults to
  68. :setting:`FILE_UPLOAD_DIRECTORY_PERMISSIONS`.
  69. The ``Storage`` class
  70. =====================
  71. .. class:: Storage
  72. The :class:`~django.core.files.storage.Storage` class provides a
  73. standardized API for storing files, along with a set of default
  74. behaviors that all other storage systems can inherit or override
  75. as necessary.
  76. .. note::
  77. When methods return naive ``datetime`` objects, the effective timezone
  78. used will be the current value of ``os.environ['TZ']``; note that this
  79. is usually set from Django's :setting:`TIME_ZONE`.
  80. .. method:: delete(name)
  81. Deletes the file referenced by ``name``. If deletion is not supported
  82. on the target storage system this will raise ``NotImplementedError``
  83. instead.
  84. .. method:: exists(name)
  85. Returns ``True`` if a file referenced by the given name already exists
  86. in the storage system, or ``False`` if the name is available for a new
  87. file.
  88. .. method:: get_accessed_time(name)
  89. Returns a :class:`~datetime.datetime` of the last accessed time of the
  90. file. For storage systems unable to return the last accessed time this
  91. will raise :exc:`NotImplementedError`.
  92. If :setting:`USE_TZ` is ``True``, returns an aware ``datetime``,
  93. otherwise returns a naive ``datetime`` in the local timezone.
  94. .. method:: get_alternative_name(file_root, file_ext)
  95. Returns an alternative filename based on the ``file_root`` and
  96. ``file_ext`` parameters, an underscore plus a random 7 character
  97. alphanumeric string is appended to the filename before the extension.
  98. .. method:: get_available_name(name, max_length=None)
  99. Returns a filename based on the ``name`` parameter that's free and
  100. available for new content to be written to on the target storage
  101. system.
  102. The length of the filename will not exceed ``max_length``, if provided.
  103. If a free unique filename cannot be found, a
  104. :exc:`SuspiciousFileOperation
  105. <django.core.exceptions.SuspiciousOperation>` exception will be raised.
  106. If a file with ``name`` already exists, :meth:`get_alternative_name` is
  107. called to obtain an alternative name.
  108. .. method:: get_created_time(name)
  109. Returns a :class:`~datetime.datetime` of the creation time of the file.
  110. For storage systems unable to return the creation time this will raise
  111. :exc:`NotImplementedError`.
  112. If :setting:`USE_TZ` is ``True``, returns an aware ``datetime``,
  113. otherwise returns a naive ``datetime`` in the local timezone.
  114. .. method:: get_modified_time(name)
  115. Returns a :class:`~datetime.datetime` of the last modified time of the
  116. file. For storage systems unable to return the last modified time this
  117. will raise :exc:`NotImplementedError`.
  118. If :setting:`USE_TZ` is ``True``, returns an aware ``datetime``,
  119. otherwise returns a naive ``datetime`` in the local timezone.
  120. .. method:: get_valid_name(name)
  121. Returns a filename based on the ``name`` parameter that's suitable
  122. for use on the target storage system.
  123. .. method:: generate_filename(filename)
  124. Validates the ``filename`` by calling :attr:`get_valid_name()` and
  125. returns a filename to be passed to the :meth:`save` method.
  126. The ``filename`` argument may include a path as returned by
  127. :attr:`FileField.upload_to <django.db.models.FileField.upload_to>`.
  128. In that case, the path won't be passed to :attr:`get_valid_name()` but
  129. will be prepended back to the resulting name.
  130. The default implementation uses :mod:`os.path` operations. Override
  131. this method if that's not appropriate for your storage.
  132. .. method:: listdir(path)
  133. Lists the contents of the specified path, returning a 2-tuple of lists;
  134. the first item being directories, the second item being files. For
  135. storage systems that aren't able to provide such a listing, this will
  136. raise a ``NotImplementedError`` instead.
  137. .. method:: open(name, mode='rb')
  138. Opens the file given by ``name``. Note that although the returned file
  139. is guaranteed to be a ``File`` object, it might actually be some
  140. subclass. In the case of remote file storage this means that
  141. reading/writing could be quite slow, so be warned.
  142. .. method:: path(name)
  143. The local filesystem path where the file can be opened using Python's
  144. standard ``open()``. For storage systems that aren't accessible from
  145. the local filesystem, this will raise ``NotImplementedError`` instead.
  146. .. method:: save(name, content, max_length=None)
  147. Saves a new file using the storage system, preferably with the name
  148. specified. If there already exists a file with this name ``name``, the
  149. storage system may modify the filename as necessary to get a unique
  150. name. The actual name of the stored file will be returned.
  151. The ``max_length`` argument is passed along to
  152. :meth:`get_available_name`.
  153. The ``content`` argument must be an instance of
  154. :class:`django.core.files.File` or a file-like object that can be
  155. wrapped in ``File``.
  156. .. method:: size(name)
  157. Returns the total size, in bytes, of the file referenced by ``name``.
  158. For storage systems that aren't able to return the file size this will
  159. raise ``NotImplementedError`` instead.
  160. .. method:: url(name)
  161. Returns the URL where the contents of the file referenced by ``name``
  162. can be accessed. For storage systems that don't support access by URL
  163. this will raise ``NotImplementedError`` instead.