storage.txt 8.2 KB

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