storage.txt 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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:: file_permissions_mode
  28. The file system permissions that the file will receive when it is
  29. saved. Defaults to :setting:`FILE_UPLOAD_PERMISSIONS`.
  30. .. versionadded:: 1.7
  31. The ``file_permissions_mode`` attribute was added. Previously files
  32. always received :setting:`FILE_UPLOAD_PERMISSIONS` permissions.
  33. .. attribute:: directory_permissions_mode
  34. The file system permissions that the directory will receive when it is
  35. saved. Defaults to :setting:`FILE_UPLOAD_DIRECTORY_PERMISSIONS`.
  36. .. versionadded:: 1.7
  37. The ``directory_permissions_mode`` attribute was added. Previously
  38. directories always received
  39. :setting:`FILE_UPLOAD_DIRECTORY_PERMISSIONS` 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. .. method:: accessed_time(name)
  51. Returns a ``datetime`` object containing the last accessed time of the
  52. file. For storage systems that aren't able to return the last accessed
  53. time this will raise ``NotImplementedError`` instead.
  54. .. method:: created_time(name)
  55. Returns a ``datetime`` object containing the creation time of the file.
  56. For storage systems that aren't able to return the creation time this
  57. will raise ``NotImplementedError`` instead.
  58. .. method:: delete(name)
  59. Deletes the file referenced by ``name``. If deletion is not supported
  60. on the target storage system this will raise ``NotImplementedError``
  61. instead
  62. .. method:: exists(name)
  63. Returns ``True`` if a file referenced by the given name already exists
  64. in the storage system, or ``False`` if the name is available for a new
  65. file.
  66. .. method:: get_available_name(name)
  67. Returns a filename based on the ``name`` parameter that's free and
  68. available for new content to be written to on the target storage
  69. system.
  70. .. method:: get_valid_name(name)
  71. Returns a filename based on the ``name`` parameter that's suitable
  72. for use on the target storage system.
  73. .. method:: listdir(path)
  74. Lists the contents of the specified path, returning a 2-tuple of lists;
  75. the first item being directories, the second item being files. For
  76. storage systems that aren't able to provide such a listing, this will
  77. raise a ``NotImplementedError`` instead.
  78. .. method:: modified_time(name)
  79. Returns a ``datetime`` object containing the last modified time. For
  80. storage systems that aren't able to return the last modified time, this
  81. will raise ``NotImplementedError`` instead.
  82. .. method:: open(name, mode='rb')
  83. Opens the file given by ``name``. Note that although the returned file
  84. is guaranteed to be a ``File`` object, it might actually be some
  85. subclass. In the case of remote file storage this means that
  86. reading/writing could be quite slow, so be warned.
  87. .. method:: path(name)
  88. The local filesystem path where the file can be opened using Python's
  89. standard ``open()``. For storage systems that aren't accessible from
  90. the local filesystem, this will raise ``NotImplementedError`` instead.
  91. .. method:: save(name, content)
  92. Saves a new file using the storage system, preferably with the name
  93. specified. If there already exists a file with this name ``name``, the
  94. storage system may modify the filename as necessary to get a unique
  95. name. The actual name of the stored file will be returned.
  96. The ``content`` argument must be an instance of
  97. :class:`django.core.files.File` or of a subclass of
  98. :class:`~django.core.files.File`.
  99. .. method:: size(name)
  100. Returns the total size, in bytes, of the file referenced by ``name``.
  101. For storage systems that aren't able to return the file size this will
  102. raise ``NotImplementedError`` instead.
  103. .. method:: url(name)
  104. Returns the URL where the contents of the file referenced by ``name``
  105. can be accessed. For storage systems that don't support access by URL
  106. this will raise ``NotImplementedError`` instead.