{"title":"Writing a custom storage system","version":"3.0","locale":"en","docname":"howto/custom-file-storage","url":"/en/3.0/howto/custom-file-storage/","canonical":"https://djangodocs.dev/en/3.0/howto/custom-file-storage/","summary":"If you need to provide custom file storage – a common example is storing files on some remote system – you can do so by defining a custom storage class. You’ll need…","html":"<h1>Writing a custom storage system<a class=\"heading-anchor\" href=\"#writing-a-custom-storage-system\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h1>\n<p>If you need to provide custom file storage – a common example is storing files\non some remote system – you can do so by defining a custom storage class.\nYou’ll need to follow these steps:</p>\n<ol class=\"arabic\">\n<li><p>Your custom storage system must be a subclass of\n<code class=\"docutils literal notranslate\"><span class=\"pre\">django.core.files.storage.Storage</span></code>:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.core.files.storage</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">Storage</span>\n\n<span class=\"k\">class</span><span class=\"w\"> </span><span class=\"nc\">MyStorage</span><span class=\"p\">(</span><span class=\"n\">Storage</span><span class=\"p\">):</span>\n    <span class=\"o\">...</span>\n</code></pre></div>\n</li>\n<li><p>Django must be able to instantiate your storage system without any arguments.\nThis means that any settings should be taken from <code class=\"docutils literal notranslate\"><span class=\"pre\">django.conf.settings</span></code>:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.conf</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">settings</span>\n<span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.core.files.storage</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">Storage</span>\n\n<span class=\"k\">class</span><span class=\"w\"> </span><span class=\"nc\">MyStorage</span><span class=\"p\">(</span><span class=\"n\">Storage</span><span class=\"p\">):</span>\n    <span class=\"k\">def</span><span class=\"w\"> </span><span class=\"fm\">__init__</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">,</span> <span class=\"n\">option</span><span class=\"o\">=</span><span class=\"kc\">None</span><span class=\"p\">):</span>\n        <span class=\"k\">if</span> <span class=\"ow\">not</span> <span class=\"n\">option</span><span class=\"p\">:</span>\n            <span class=\"n\">option</span> <span class=\"o\">=</span> <span class=\"n\">settings</span><span class=\"o\">.</span><span class=\"n\">CUSTOM_STORAGE_OPTIONS</span>\n        <span class=\"o\">...</span>\n</code></pre></div>\n</li>\n<li><p>Your storage class must implement the <a class=\"reference internal\" href=\"#django.core.files.storage._open\" title=\"django.core.files.storage._open\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">_open()</span></code></a> and <a class=\"reference internal\" href=\"#django.core.files.storage._save\" title=\"django.core.files.storage._save\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">_save()</span></code></a>\nmethods, along with any other methods appropriate to your storage class. See\nbelow for more on these methods.</p>\n<p>In addition, if your class provides local file storage, it must override\nthe <code class=\"docutils literal notranslate\"><span class=\"pre\">path()</span></code> method.</p>\n</li>\n<li><p>Your storage class must be <a class=\"reference internal\" href=\"/en/3.0/topics/migrations/#custom-deconstruct-method\"><span class=\"std std-ref\">deconstructible</span></a>\nso it can be serialized when it’s used on a field in a migration. As long\nas your field has arguments that are themselves\n<a class=\"reference internal\" href=\"/en/3.0/topics/migrations/#migration-serializing\"><span class=\"std std-ref\">serializable</span></a>, you can use the\n<code class=\"docutils literal notranslate\"><span class=\"pre\">django.utils.deconstruct.deconstructible</span></code> class decorator for this\n(that’s what Django uses on FileSystemStorage).</p></li>\n</ol>\n<p>By default, the following methods raise <code class=\"docutils literal notranslate\"><span class=\"pre\">NotImplementedError</span></code> and will\ntypically have to be overridden:</p>\n<ul class=\"simple\">\n<li><p><a class=\"reference internal\" href=\"/en/3.0/ref/files/storage/#django.core.files.storage.Storage.delete\" title=\"django.core.files.storage.Storage.delete\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">Storage.delete()</span></code></a></p></li>\n<li><p><a class=\"reference internal\" href=\"/en/3.0/ref/files/storage/#django.core.files.storage.Storage.exists\" title=\"django.core.files.storage.Storage.exists\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">Storage.exists()</span></code></a></p></li>\n<li><p><a class=\"reference internal\" href=\"/en/3.0/ref/files/storage/#django.core.files.storage.Storage.listdir\" title=\"django.core.files.storage.Storage.listdir\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">Storage.listdir()</span></code></a></p></li>\n<li><p><a class=\"reference internal\" href=\"/en/3.0/ref/files/storage/#django.core.files.storage.Storage.size\" title=\"django.core.files.storage.Storage.size\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">Storage.size()</span></code></a></p></li>\n<li><p><a class=\"reference internal\" href=\"/en/3.0/ref/files/storage/#django.core.files.storage.Storage.url\" title=\"django.core.files.storage.Storage.url\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">Storage.url()</span></code></a></p></li>\n</ul>\n<p>Note however that not all these methods are required and may be deliberately\nomitted. As it happens, it is possible to leave each method unimplemented and\nstill have a working Storage.</p>\n<p>By way of example, if listing the contents of certain storage backends turns\nout to be expensive, you might decide not to implement <code class=\"docutils literal notranslate\"><span class=\"pre\">Storage.listdir()</span></code>.</p>\n<p>Another example would be a backend that only handles writing to files. In this\ncase, you would not need to implement any of the above methods.</p>\n<p>Ultimately, which of these methods are implemented is up to you. Leaving some\nmethods unimplemented will result in a partial (possibly broken) interface.</p>\n<p>You’ll also usually want to use hooks specifically designed for custom storage\nobjects. These are:</p>\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.core.files.storage._open\">\n<span class=\"sig-name descname\"><span class=\"pre\">_open</span></span><span class=\"sig-paren\">(</span><em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">name</span></span></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">mode</span></span><span class=\"o\"><span class=\"pre\">=</span></span><span class=\"default_value\"><span class=\"pre\">'rb'</span></span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.core.files.storage._open\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p><strong>Required</strong>.</p>\n<p>Called by <code class=\"docutils literal notranslate\"><span class=\"pre\">Storage.open()</span></code>, this is the actual mechanism the storage class\nuses to open the file. This must return a <code class=\"docutils literal notranslate\"><span class=\"pre\">File</span></code> object, though in most cases,\nyou’ll want to return some subclass here that implements logic specific to the\nbackend storage system.</p>\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.core.files.storage._save\">\n<span class=\"sig-name descname\"><span class=\"pre\">_save</span></span><span class=\"sig-paren\">(</span><em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">name</span></span></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">content</span></span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.core.files.storage._save\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>Called by <code class=\"docutils literal notranslate\"><span class=\"pre\">Storage.save()</span></code>. The <code class=\"docutils literal notranslate\"><span class=\"pre\">name</span></code> will already have gone through\n<code class=\"docutils literal notranslate\"><span class=\"pre\">get_valid_name()</span></code> and <code class=\"docutils literal notranslate\"><span class=\"pre\">get_available_name()</span></code>, and the <code class=\"docutils literal notranslate\"><span class=\"pre\">content</span></code> will be a\n<code class=\"docutils literal notranslate\"><span class=\"pre\">File</span></code> object itself.</p>\n<p>Should return the actual name of name of the file saved (usually the <code class=\"docutils literal notranslate\"><span class=\"pre\">name</span></code>\npassed in, but if the storage needs to change the file name return the new name\ninstead).</p>\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.core.files.storage.get_valid_name\">\n<span class=\"sig-name descname\"><span class=\"pre\">get_valid_name</span></span><span class=\"sig-paren\">(</span><em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">name</span></span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.core.files.storage.get_valid_name\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>Returns a filename suitable for use with the underlying storage system. The\n<code class=\"docutils literal notranslate\"><span class=\"pre\">name</span></code> argument passed to this method is either the original filename sent to\nthe server or, if <code class=\"docutils literal notranslate\"><span class=\"pre\">upload_to</span></code> is a callable, the filename returned by that\nmethod after any path information is removed. Override this to customize how\nnon-standard characters are converted to safe filenames.</p>\n<p>The code provided on <code class=\"docutils literal notranslate\"><span class=\"pre\">Storage</span></code> retains only alpha-numeric characters, periods\nand underscores from the original filename, removing everything else.</p>\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.core.files.storage.get_alternative_name\">\n<span class=\"sig-name descname\"><span class=\"pre\">get_alternative_name</span></span><span class=\"sig-paren\">(</span><em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">file_root</span></span></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">file_ext</span></span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.core.files.storage.get_alternative_name\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<aside class=\"version-note version-added\" data-version=\"3.0\">\n<p class=\"version-note-title\">New in Django 3.0</p></aside>\n<p>Returns an alternative filename based on the <code class=\"docutils literal notranslate\"><span class=\"pre\">file_root</span></code> and <code class=\"docutils literal notranslate\"><span class=\"pre\">file_ext</span></code>\nparameters. By default, an underscore plus a random 7 character alphanumeric\nstring is appended to the filename before the extension.</p>\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.core.files.storage.get_available_name\">\n<span class=\"sig-name descname\"><span class=\"pre\">get_available_name</span></span><span class=\"sig-paren\">(</span><em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">name</span></span></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">max_length</span></span><span class=\"o\"><span class=\"pre\">=</span></span><span class=\"default_value\"><span class=\"pre\">None</span></span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.core.files.storage.get_available_name\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>Returns a filename that is available in the storage mechanism, possibly taking\nthe provided filename into account. The <code class=\"docutils literal notranslate\"><span class=\"pre\">name</span></code> argument passed to this method\nwill have already cleaned to a filename valid for the storage system, according\nto the <code class=\"docutils literal notranslate\"><span class=\"pre\">get_valid_name()</span></code> method described above.</p>\n<p>The length of the filename will not exceed <code class=\"docutils literal notranslate\"><span class=\"pre\">max_length</span></code>, if provided. If a\nfree unique filename cannot be found, a <a class=\"reference internal\" href=\"/en/3.0/ref/exceptions/#django.core.exceptions.SuspiciousOperation\" title=\"django.core.exceptions.SuspiciousOperation\"><code class=\"xref py py-exc docutils literal notranslate\"><span class=\"pre\">SuspiciousFileOperation</span></code></a> exception is raised.</p>\n<p>If a file with <code class=\"docutils literal notranslate\"><span class=\"pre\">name</span></code> already exists, <code class=\"docutils literal notranslate\"><span class=\"pre\">get_alternative_name()</span></code> is called to\nobtain an alternative name.</p>","rootId":"writing-a-custom-storage-system","toc":[],"breadcrumbs":[{"docname":"howto/index","title":"“How-to” guides","url":"/en/3.0/howto/"}],"prev":{"docname":"howto/custom-template-tags","title":"Custom template tags and filters","url":"/en/3.0/howto/custom-template-tags/"},"next":{"docname":"howto/deployment/index","title":"Deploying Django","url":"/en/3.0/howto/deployment/"},"formats":{"html":"/en/3.0/howto/custom-file-storage/","markdown":"/en/3.0/howto/custom-file-storage.md","json":"/en/3.0/howto/custom-file-storage.json"},"source":"https://github.com/django/django/blob/stable/3.0.x/docs/howto/custom-file-storage.txt","official":"https://docs.djangoproject.com/en/3.0/howto/custom-file-storage/","inVersions":["dev","6.1","6.0","5.2","5.1","5.0","4.2","4.1","4.0","3.2","3.1","3.0","2.2","2.1","2.0","1.11","1.10","1.9","1.8"],"inLocales":["en","zh-hans","fr","ja","id","pt-br","ko","es","el","pl"]}