storage.txt 9.7 KB

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