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