storage.txt 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. The ``Storage`` class
  44. =====================
  45. .. class:: Storage
  46. The :class:`~django.core.files.storage.Storage` class provides a
  47. standardized API for storing files, along with a set of default
  48. behaviors that all other storage systems can inherit or override
  49. as necessary.
  50. .. note::
  51. For methods returning naive ``datetime`` objects, the
  52. effective timezone used will be the current value of
  53. ``os.environ['TZ']``; note that this is usually set from
  54. Django's :setting:`TIME_ZONE`.
  55. .. method:: accessed_time(name)
  56. Returns a naive ``datetime`` object containing the last
  57. accessed time of the file. For storage systems that aren't
  58. able to return the last accessed time this will raise
  59. ``NotImplementedError`` instead.
  60. .. method:: created_time(name)
  61. Returns a naive ``datetime`` object containing the creation
  62. time of the file. For storage systems that aren't able to
  63. return the creation time this will raise
  64. ``NotImplementedError`` instead.
  65. .. method:: delete(name)
  66. Deletes the file referenced by ``name``. If deletion is not supported
  67. on the target storage system this will raise ``NotImplementedError``
  68. instead
  69. .. method:: exists(name)
  70. Returns ``True`` if a file referenced by the given name already exists
  71. in the storage system, or ``False`` if the name is available for a new
  72. file.
  73. .. method:: get_available_name(name, max_length=None)
  74. Returns a filename based on the ``name`` parameter that's free and
  75. available for new content to be written to on the target storage
  76. system.
  77. The length of the filename will not exceed ``max_length``, if provided.
  78. If a free unique filename cannot be found, a
  79. :exc:`SuspiciousFileOperation
  80. <django.core.exceptions.SuspiciousOperation>` exception will be raised.
  81. If a file with ``name`` already exists, an underscore plus a random
  82. 7 character alphanumeric string is appended to the filename before
  83. the extension.
  84. .. method:: get_valid_name(name)
  85. Returns a filename based on the ``name`` parameter that's suitable
  86. for use on the target storage system.
  87. .. method:: listdir(path)
  88. Lists the contents of the specified path, returning a 2-tuple of lists;
  89. the first item being directories, the second item being files. For
  90. storage systems that aren't able to provide such a listing, this will
  91. raise a ``NotImplementedError`` instead.
  92. .. method:: modified_time(name)
  93. Returns a naive ``datetime`` object containing the last
  94. modified time. For storage systems that aren't able to return
  95. the last modified time, this will raise
  96. ``NotImplementedError`` instead.
  97. .. method:: open(name, mode='rb')
  98. Opens the file given by ``name``. Note that although the returned file
  99. is guaranteed to be a ``File`` object, it might actually be some
  100. subclass. In the case of remote file storage this means that
  101. reading/writing could be quite slow, so be warned.
  102. .. method:: path(name)
  103. The local filesystem path where the file can be opened using Python's
  104. standard ``open()``. For storage systems that aren't accessible from
  105. the local filesystem, this will raise ``NotImplementedError`` instead.
  106. .. method:: save(name, content, max_length=None)
  107. Saves a new file using the storage system, preferably with the name
  108. specified. If there already exists a file with this name ``name``, the
  109. storage system may modify the filename as necessary to get a unique
  110. name. The actual name of the stored file will be returned.
  111. The ``max_length`` argument is passed along to
  112. :meth:`get_available_name`.
  113. The ``content`` argument must be an instance of
  114. :class:`django.core.files.File` or of a subclass of
  115. :class:`~django.core.files.File`.
  116. .. method:: size(name)
  117. Returns the total size, in bytes, of the file referenced by ``name``.
  118. For storage systems that aren't able to return the file size this will
  119. raise ``NotImplementedError`` instead.
  120. .. method:: url(name)
  121. Returns the URL where the contents of the file referenced by ``name``
  122. can be accessed. For storage systems that don't support access by URL
  123. this will raise ``NotImplementedError`` instead.