<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>File storage API — Django 1.5.9 documentation</title> <link rel="stylesheet" href="../../_static/default.css" type="text/css" /> <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" /> <script type="text/javascript"> var DOCUMENTATION_OPTIONS = { URL_ROOT: '../../', VERSION: '1.5.9', COLLAPSE_INDEX: false, FILE_SUFFIX: '.html', HAS_SOURCE: true }; </script> <script type="text/javascript" src="../../_static/jquery.js"></script> <script type="text/javascript" src="../../_static/underscore.js"></script> <script type="text/javascript" src="../../_static/doctools.js"></script> <link rel="top" title="Django 1.5.9 documentation" href="../../index.html" /> <link rel="up" title="File handling" href="index.html" /> <link rel="next" title="Forms" href="../forms/index.html" /> <link rel="prev" title="The File object" href="file.html" /> <script type="text/javascript" src="../../templatebuiltins.js"></script> <script type="text/javascript"> (function($) { if (!django_template_builtins) { // templatebuiltins.js missing, do nothing. return; } $(document).ready(function() { // Hyperlink Django template tags and filters var base = "../templates/builtins.html"; if (base == "#") { // Special case for builtins.html itself base = ""; } // Tags are keywords, class '.k' $("div.highlight\\-html\\+django span.k").each(function(i, elem) { var tagname = $(elem).text(); if ($.inArray(tagname, django_template_builtins.ttags) != -1) { var fragment = tagname.replace(/_/, '-'); $(elem).html("<a href='" + base + "#" + fragment + "'>" + tagname + "</a>"); } }); // Filters are functions, class '.nf' $("div.highlight\\-html\\+django span.nf").each(function(i, elem) { var filtername = $(elem).text(); if ($.inArray(filtername, django_template_builtins.tfilters) != -1) { var fragment = filtername.replace(/_/, '-'); $(elem).html("<a href='" + base + "#" + fragment + "'>" + filtername + "</a>"); } }); }); })(jQuery); </script> </head> <body> <div class="document"> <div id="custom-doc" class="yui-t6"> <div id="hd"> <h1><a href="../../index.html">Django 1.5.9 documentation</a></h1> <div id="global-nav"> <a title="Home page" href="../../index.html">Home</a> | <a title="Table of contents" href="../../contents.html">Table of contents</a> | <a title="Global index" href="../../genindex.html">Index</a> | <a title="Module index" href="../../py-modindex.html">Modules</a> </div> <div class="nav"> « <a href="file.html" title="The <tt class="docutils literal"><span class="pre">File</span></tt> object">previous</a> | <a href="../index.html" title="API Reference" accesskey="U">up</a> | <a href="../forms/index.html" title="Forms">next</a> »</div> </div> <div id="bd"> <div id="yui-main"> <div class="yui-b"> <div class="yui-g" id="ref-files-storage"> <div class="section" id="s-module-django.core.files.storage"> <span id="s-file-storage-api"></span><span id="module-django.core.files.storage"></span><span id="file-storage-api"></span><h1>File storage API<a class="headerlink" href="#module-django.core.files.storage" title="Permalink to this headline">¶</a></h1> <div class="section" id="s-getting-the-current-storage-class"> <span id="getting-the-current-storage-class"></span><h2>Getting the current storage class<a class="headerlink" href="#getting-the-current-storage-class" title="Permalink to this headline">¶</a></h2> <p>Django provides two convenient ways to access the current storage class:</p> <dl class="class"> <dt id="django.core.files.storage.DefaultStorage"> <em class="property">class </em><tt class="descname">DefaultStorage</tt><a class="headerlink" href="#django.core.files.storage.DefaultStorage" title="Permalink to this definition">¶</a></dt> <dd><p><a class="reference internal" href="#django.core.files.storage.DefaultStorage" title="django.core.files.storage.DefaultStorage"><tt class="xref py py-class docutils literal"><span class="pre">DefaultStorage</span></tt></a> provides lazy access to the current default storage system as defined by <a class="reference internal" href="../settings.html#std:setting-DEFAULT_FILE_STORAGE"><tt class="xref std std-setting docutils literal"><span class="pre">DEFAULT_FILE_STORAGE</span></tt></a>. <a class="reference internal" href="#django.core.files.storage.DefaultStorage" title="django.core.files.storage.DefaultStorage"><tt class="xref py py-class docutils literal"><span class="pre">DefaultStorage</span></tt></a> uses <a class="reference internal" href="#django.core.files.storage.get_storage_class" title="django.core.files.storage.get_storage_class"><tt class="xref py py-func docutils literal"><span class="pre">get_storage_class()</span></tt></a> internally.</p> </dd></dl> <dl class="function"> <dt id="django.core.files.storage.get_storage_class"> <tt class="descname">get_storage_class</tt>(<span class="optional">[</span><em>import_path=None</em><span class="optional">]</span>)<a class="headerlink" href="#django.core.files.storage.get_storage_class" title="Permalink to this definition">¶</a></dt> <dd><p>Returns a class or module which implements the storage API.</p> <p>When called without the <tt class="docutils literal"><span class="pre">import_path</span></tt> parameter <tt class="docutils literal"><span class="pre">get_storage_class</span></tt> will return the current default storage system as defined by <a class="reference internal" href="../settings.html#std:setting-DEFAULT_FILE_STORAGE"><tt class="xref std std-setting docutils literal"><span class="pre">DEFAULT_FILE_STORAGE</span></tt></a>. If <tt class="docutils literal"><span class="pre">import_path</span></tt> is provided, <tt class="docutils literal"><span class="pre">get_storage_class</span></tt> will attempt to import the class or module from the given path and will return it if successful. An exception will be raised if the import is unsuccessful.</p> </dd></dl> </div> <div class="section" id="s-the-filesystemstorage-class"> <span id="the-filesystemstorage-class"></span><h2>The FileSystemStorage Class<a class="headerlink" href="#the-filesystemstorage-class" title="Permalink to this headline">¶</a></h2> <dl class="class"> <dt id="django.core.files.storage.FileSystemStorage"> <em class="property">class </em><tt class="descname">FileSystemStorage</tt><a class="headerlink" href="#django.core.files.storage.FileSystemStorage" title="Permalink to this definition">¶</a></dt> <dd><p>The <a class="reference internal" href="#django.core.files.storage.FileSystemStorage" title="django.core.files.storage.FileSystemStorage"><tt class="xref py py-class docutils literal"><span class="pre">FileSystemStorage</span></tt></a> class implements basic file storage on a local filesystem. It inherits from <a class="reference internal" href="#django.core.files.storage.Storage" title="django.core.files.storage.Storage"><tt class="xref py py-class docutils literal"><span class="pre">Storage</span></tt></a> and provides implementations for all the public methods thereof.</p> <div class="admonition note"> <p class="first admonition-title">Note</p> <p class="last">The <tt class="docutils literal"><span class="pre">FileSystemStorage.delete()</span></tt> method will not raise an exception if the given file name does not exist.</p> </div> </dd></dl> </div> <div class="section" id="s-the-storage-class"> <span id="the-storage-class"></span><h2>The Storage Class<a class="headerlink" href="#the-storage-class" title="Permalink to this headline">¶</a></h2> <dl class="class"> <dt id="django.core.files.storage.Storage"> <em class="property">class </em><tt class="descname">Storage</tt><a class="headerlink" href="#django.core.files.storage.Storage" title="Permalink to this definition">¶</a></dt> <dd><p>The <a class="reference internal" href="#django.core.files.storage.Storage" title="django.core.files.storage.Storage"><tt class="xref py py-class docutils literal"><span class="pre">Storage</span></tt></a> class provides a standardized API for storing files, along with a set of default behaviors that all other storage systems can inherit or override as necessary.</p> <dl class="method"> <dt id="django.core.files.storage.Storage.accessed_time"> <tt class="descname">accessed_time</tt>(<em>name</em>)<a class="headerlink" href="#django.core.files.storage.Storage.accessed_time" title="Permalink to this definition">¶</a></dt> <dd><p>Returns a <tt class="docutils literal"><span class="pre">datetime</span></tt> object containing the last accessed time of the file. For storage systems that aren’t able to return the last accessed time this will raise <tt class="docutils literal"><span class="pre">NotImplementedError</span></tt> instead.</p> </dd></dl> <dl class="method"> <dt id="django.core.files.storage.Storage.created_time"> <tt class="descname">created_time</tt>(<em>name</em>)<a class="headerlink" href="#django.core.files.storage.Storage.created_time" title="Permalink to this definition">¶</a></dt> <dd><p>Returns a <tt class="docutils literal"><span class="pre">datetime</span></tt> object containing the creation time of the file. For storage systems that aren’t able to return the creation time this will raise <tt class="docutils literal"><span class="pre">NotImplementedError</span></tt> instead.</p> </dd></dl> <dl class="method"> <dt id="django.core.files.storage.Storage.delete"> <tt class="descname">delete</tt>(<em>name</em>)<a class="headerlink" href="#django.core.files.storage.Storage.delete" title="Permalink to this definition">¶</a></dt> <dd><p>Deletes the file referenced by <tt class="docutils literal"><span class="pre">name</span></tt>. If deletion is not supported on the target storage system this will raise <tt class="docutils literal"><span class="pre">NotImplementedError</span></tt> instead</p> </dd></dl> <dl class="method"> <dt id="django.core.files.storage.Storage.exists"> <tt class="descname">exists</tt>(<em>name</em>)<a class="headerlink" href="#django.core.files.storage.Storage.exists" title="Permalink to this definition">¶</a></dt> <dd><p>Returns <tt class="docutils literal"><span class="pre">True</span></tt> if a file referenced by the given name already exists in the storage system, or <tt class="docutils literal"><span class="pre">False</span></tt> if the name is available for a new file.</p> </dd></dl> <dl class="method"> <dt id="django.core.files.storage.Storage.get_available_name"> <tt class="descname">get_available_name</tt>(<em>name</em>)<a class="headerlink" href="#django.core.files.storage.Storage.get_available_name" title="Permalink to this definition">¶</a></dt> <dd><p>Returns a filename based on the <tt class="docutils literal"><span class="pre">name</span></tt> parameter that’s free and available for new content to be written to on the target storage system.</p> <div class="versionchanged"> <span class="title">Changed in Django 1.5.9.</span> </div> <p>If a file with <tt class="docutils literal"><span class="pre">name</span></tt> already exists, an underscore plus a random 7 character alphanumeric string is appended to the filename before the extension.</p> <p>Previously, an underscore followed by a number (e.g. <tt class="docutils literal"><span class="pre">"_1"</span></tt>, <tt class="docutils literal"><span class="pre">"_2"</span></tt>, etc.) was appended to the filename until an avaible name in the destination directory was found. A malicious user could exploit this deterministic algorithm to create a denial-of-service attack. This change was also made in Django 1.4.14.</p> </dd></dl> <dl class="method"> <dt id="django.core.files.storage.Storage.get_valid_name"> <tt class="descname">get_valid_name</tt>(<em>name</em>)<a class="headerlink" href="#django.core.files.storage.Storage.get_valid_name" title="Permalink to this definition">¶</a></dt> <dd><p>Returns a filename based on the <tt class="docutils literal"><span class="pre">name</span></tt> parameter that’s suitable for use on the target storage system.</p> </dd></dl> <dl class="method"> <dt id="django.core.files.storage.Storage.listdir"> <tt class="descname">listdir</tt>(<em>path</em>)<a class="headerlink" href="#django.core.files.storage.Storage.listdir" title="Permalink to this definition">¶</a></dt> <dd><p>Lists the contents of the specified path, returning a 2-tuple of lists; the first item being directories, the second item being files. For storage systems that aren’t able to provide such a listing, this will raise a <tt class="docutils literal"><span class="pre">NotImplementedError</span></tt> instead.</p> </dd></dl> <dl class="method"> <dt id="django.core.files.storage.Storage.modified_time"> <tt class="descname">modified_time</tt>(<em>name</em>)<a class="headerlink" href="#django.core.files.storage.Storage.modified_time" title="Permalink to this definition">¶</a></dt> <dd><p>Returns a <tt class="docutils literal"><span class="pre">datetime</span></tt> object containing the last modified time. For storage systems that aren’t able to return the last modified time, this will raise <tt class="docutils literal"><span class="pre">NotImplementedError</span></tt> instead.</p> </dd></dl> <dl class="method"> <dt id="django.core.files.storage.Storage.open"> <tt class="descname">open</tt>(<em>name</em>, <em>mode='rb'</em>)<a class="headerlink" href="#django.core.files.storage.Storage.open" title="Permalink to this definition">¶</a></dt> <dd><p>Opens the file given by <tt class="docutils literal"><span class="pre">name</span></tt>. Note that although the returned file is guaranteed to be a <tt class="docutils literal"><span class="pre">File</span></tt> object, it might actually be some subclass. In the case of remote file storage this means that reading/writing could be quite slow, so be warned.</p> </dd></dl> <dl class="method"> <dt id="django.core.files.storage.Storage.path"> <tt class="descname">path</tt>(<em>name</em>)<a class="headerlink" href="#django.core.files.storage.Storage.path" title="Permalink to this definition">¶</a></dt> <dd><p>The local filesystem path where the file can be opened using Python’s standard <tt class="docutils literal"><span class="pre">open()</span></tt>. For storage systems that aren’t accessible from the local filesystem, this will raise <tt class="docutils literal"><span class="pre">NotImplementedError</span></tt> instead.</p> </dd></dl> <dl class="method"> <dt id="django.core.files.storage.Storage.save"> <tt class="descname">save</tt>(<em>name</em>, <em>content</em>)<a class="headerlink" href="#django.core.files.storage.Storage.save" title="Permalink to this definition">¶</a></dt> <dd><p>Saves a new file using the storage system, preferably with the name specified. If there already exists a file with this name <tt class="docutils literal"><span class="pre">name</span></tt>, the storage system may modify the filename as necessary to get a unique name. The actual name of the stored file will be returned.</p> <p>The <tt class="docutils literal"><span class="pre">content</span></tt> argument must be an instance of <a class="reference internal" href="file.html#django.core.files.File" title="django.core.files.File"><tt class="xref py py-class docutils literal"><span class="pre">django.core.files.File</span></tt></a> or of a subclass of <a class="reference internal" href="file.html#django.core.files.File" title="django.core.files.File"><tt class="xref py py-class docutils literal"><span class="pre">File</span></tt></a>.</p> </dd></dl> <dl class="method"> <dt id="django.core.files.storage.Storage.size"> <tt class="descname">size</tt>(<em>name</em>)<a class="headerlink" href="#django.core.files.storage.Storage.size" title="Permalink to this definition">¶</a></dt> <dd><p>Returns the total size, in bytes, of the file referenced by <tt class="docutils literal"><span class="pre">name</span></tt>. For storage systems that aren’t able to return the file size this will raise <tt class="docutils literal"><span class="pre">NotImplementedError</span></tt> instead.</p> </dd></dl> <dl class="method"> <dt id="django.core.files.storage.Storage.url"> <tt class="descname">url</tt>(<em>name</em>)<a class="headerlink" href="#django.core.files.storage.Storage.url" title="Permalink to this definition">¶</a></dt> <dd><p>Returns the URL where the contents of the file referenced by <tt class="docutils literal"><span class="pre">name</span></tt> can be accessed. For storage systems that don’t support access by URL this will raise <tt class="docutils literal"><span class="pre">NotImplementedError</span></tt> instead.</p> </dd></dl> </dd></dl> </div> </div> </div> </div> </div> <div class="yui-b" id="sidebar"> <div class="sphinxsidebar"> <div class="sphinxsidebarwrapper"> <h3><a href="../../contents.html">Table Of Contents</a></h3> <ul> <li><a class="reference internal" href="#">File storage API</a><ul> <li><a class="reference internal" href="#getting-the-current-storage-class">Getting the current storage class</a></li> <li><a class="reference internal" href="#the-filesystemstorage-class">The FileSystemStorage Class</a></li> <li><a class="reference internal" href="#the-storage-class">The Storage Class</a></li> </ul> </li> </ul> <h3>Browse</h3> <ul> <li>Prev: <a href="file.html">The <tt class="docutils literal"><span class="pre">File</span></tt> object</a></li> <li>Next: <a href="../forms/index.html">Forms</a></li> </ul> <h3>You are here:</h3> <ul> <li> <a href="../../index.html">Django 1.5.9 documentation</a> <ul><li><a href="../index.html">API Reference</a> <ul><li><a href="index.html">File handling</a> <ul><li>File storage API</li></ul> </li></ul></li></ul> </li> </ul> <h3>This Page</h3> <ul class="this-page-menu"> <li><a href="../../_sources/ref/files/storage.txt" rel="nofollow">Show Source</a></li> </ul> <div id="searchbox" style="display: none"> <h3>Quick search</h3> <form class="search" action="../../search.html" method="get"> <input type="text" name="q" /> <input type="submit" value="Go" /> <input type="hidden" name="check_keywords" value="yes" /> <input type="hidden" name="area" value="default" /> </form> <p class="searchtip" style="font-size: 90%"> Enter search terms or a module, class or function name. </p> </div> <script type="text/javascript">$('#searchbox').show(0);</script> </div> </div> <h3>Last update:</h3> <p class="topless">Aug 21, 2014</p> </div> </div> <div id="ft"> <div class="nav"> « <a href="file.html" title="The <tt class="docutils literal"><span class="pre">File</span></tt> object">previous</a> | <a href="../index.html" title="API Reference" accesskey="U">up</a> | <a href="../forms/index.html" title="Forms">next</a> »</div> </div> </div> <div class="clearer"></div> </div> </body> </html>