storage.txt 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. File storage API
  2. ================
  3. .. module:: django.core.files.storage
  4. Getting the current storage class
  5. ---------------------------------
  6. Django provides two convenient ways to access the current storage class:
  7. .. class:: DefaultStorage
  8. :class:`~django.core.files.storage.DefaultStorage` provides
  9. lazy access to the current default storage system as defined by
  10. :setting:`DEFAULT_FILE_STORAGE`. :class:`DefaultStorage` uses
  11. :func:`~django.core.files.storage.get_storage_class` internally.
  12. .. function:: get_storage_class(import_path=None)
  13. Returns a class or module which implements the storage API.
  14. When called without the ``import_path`` parameter ``get_storage_class``
  15. will return the current default storage system as defined by
  16. :setting:`DEFAULT_FILE_STORAGE`. If ``import_path`` is provided,
  17. ``get_storage_class`` will attempt to import the class or module from the
  18. given path and will return it if successful. An exception will be
  19. raised if the import is unsuccessful.
  20. The FileSystemStorage Class
  21. ---------------------------
  22. .. class:: FileSystemStorage(location=None, base_url=None, file_permissions_mode=None, directory_permissions_mode=None)
  23. The :class:`~django.core.files.storage.FileSystemStorage` class implements
  24. basic file storage on a local filesystem. It inherits from
  25. :class:`~django.core.files.storage.Storage` and provides implementations
  26. for all the public methods thereof.
  27. .. attribute:: location
  28. Absolute path to the directory that will hold the files.
  29. Defaults to the value of your :setting:`MEDIA_ROOT` setting.
  30. .. attribute:: base_url
  31. URL that serves the files stored at this location.
  32. Defaults to the value of your :setting:`MEDIA_URL` setting.
  33. .. attribute:: file_permissions_mode
  34. The file system permissions that the file will receive when it is
  35. saved. Defaults to :setting:`FILE_UPLOAD_PERMISSIONS`.
  36. .. attribute:: directory_permissions_mode
  37. The file system permissions that the directory will receive when it is
  38. saved. Defaults to :setting:`FILE_UPLOAD_DIRECTORY_PERMISSIONS`.
  39. .. note::
  40. The ``FileSystemStorage.delete()`` method will not raise
  41. an exception if the given file name does not exist.
  42. The Storage Class
  43. -----------------
  44. .. class:: Storage
  45. The :class:`~django.core.files.storage.Storage` class provides a
  46. standardized API for storing files, along with a set of default
  47. behaviors that all other storage systems can inherit or override
  48. as necessary.
  49. .. note::
  50. For methods returning naive ``datetime`` objects, the
  51. effective timezone used will be the current value of
  52. ``os.environ['TZ']``; note that this is usually set from
  53. Django's :setting:`TIME_ZONE`.
  54. .. method:: accessed_time(name)
  55. Returns a naive ``datetime`` object containing the last
  56. accessed time of the file. For storage systems that aren't
  57. able to return the last accessed time this will raise
  58. ``NotImplementedError`` instead.
  59. .. method:: created_time(name)
  60. Returns a naive ``datetime`` object containing the creation
  61. time of the file. For storage systems that aren't able to
  62. return the creation time this will raise
  63. ``NotImplementedError`` instead.
  64. .. method:: delete(name)
  65. Deletes the file referenced by ``name``. If deletion is not supported
  66. on the target storage system this will raise ``NotImplementedError``
  67. instead
  68. .. method:: exists(name)
  69. Returns ``True`` if a file referenced by the given name already exists
  70. in the storage system, or ``False`` if the name is available for a new
  71. file.
  72. .. method:: get_available_name(name, max_length=None)
  73. Returns a filename based on the ``name`` parameter that's free and
  74. available for new content to be written to on the target storage
  75. system.
  76. The length of the filename will not exceed ``max_length``, if provided.
  77. If a free unique filename cannot be found, a
  78. :exc:`SuspiciousFileOperation
  79. <django.core.exceptions.SuspiciousOperation>` exception will be raised.
  80. If a file with ``name`` already exists, an underscore plus a random
  81. 7 character alphanumeric string is appended to the filename before
  82. the extension.
  83. .. versionchanged:: 1.8
  84. The ``max_length`` argument was added.
  85. .. method:: get_valid_name(name)
  86. Returns a filename based on the ``name`` parameter that's suitable
  87. for use on the target storage system.
  88. .. method:: listdir(path)
  89. Lists the contents of the specified path, returning a 2-tuple of lists;
  90. the first item being directories, the second item being files. For
  91. storage systems that aren't able to provide such a listing, this will
  92. raise a ``NotImplementedError`` instead.
  93. .. method:: modified_time(name)
  94. Returns a naive ``datetime`` object containing the last
  95. modified time. For storage systems that aren't able to return
  96. the last modified time, this will raise
  97. ``NotImplementedError`` instead.
  98. .. method:: open(name, mode='rb')
  99. Opens the file given by ``name``. Note that although the returned file
  100. is guaranteed to be a ``File`` object, it might actually be some
  101. subclass. In the case of remote file storage this means that
  102. reading/writing could be quite slow, so be warned.
  103. .. method:: path(name)
  104. The local filesystem path where the file can be opened using Python's
  105. standard ``open()``. For storage systems that aren't accessible from
  106. the local filesystem, this will raise ``NotImplementedError`` instead.
  107. .. method:: save(name, content, max_length=None)
  108. Saves a new file using the storage system, preferably with the name
  109. specified. If there already exists a file with this name ``name``, the
  110. storage system may modify the filename as necessary to get a unique
  111. name. The actual name of the stored file will be returned.
  112. The ``max_length`` argument is passed along to
  113. :meth:`get_available_name`.
  114. The ``content`` argument must be an instance of
  115. :class:`django.core.files.File` or of a subclass of
  116. :class:`~django.core.files.File`.
  117. .. versionchanged:: 1.8
  118. The ``max_length`` argument was added.
  119. .. method:: size(name)
  120. Returns the total size, in bytes, of the file referenced by ``name``.
  121. For storage systems that aren't able to return the file size this will
  122. raise ``NotImplementedError`` instead.
  123. .. method:: url(name)
  124. Returns the URL where the contents of the file referenced by ``name``
  125. can be accessed. For storage systems that don't support access by URL
  126. this will raise ``NotImplementedError`` instead.