{"title":"How to write a custom storage class","version":"4.2","locale":"el","docname":"howto/custom-file-storage","url":"/el/4.2/howto/custom-file-storage/","canonical":"https://djangodocs.dev/el/4.2/howto/custom-file-storage/","summary":"Αν θέλετε να έχετε το δικό σας σύστημα αποθήκευσης αρχείων – ένα συνηθισμένο παράδειγμα είναι η αποθήκευση αρχείων σε ένα απομακρυσμένο σύστημα – μπορείτε…","html":"<h1>How to write a custom storage class<a class=\"heading-anchor\" href=\"#how-to-write-a-custom-storage-class\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h1>\n<p>Αν θέλετε να έχετε το δικό σας σύστημα αποθήκευσης αρχείων – ένα συνηθισμένο παράδειγμα είναι η αποθήκευση αρχείων σε ένα απομακρυσμένο σύστημα – μπορείτε να το κάνετε ορίζοντας μια δικιά σας κλάση αποθήκευσης (storage class). Θα χρειαστεί να ακολουθήσετε τα παρακάτω βήματα:</p>\n<ol class=\"arabic\">\n<li><p>Το δικό σας σύστημα αποθήκευσης θα πρέπει να είναι μια subclass της κλάσης <code class=\"docutils literal notranslate\">django.core.files.storage.Storage</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=\"nn\">django.core.files.storage</span> <span class=\"kn\">import</span> Storage\n\n\n<span class=\"k\">class</span> <span class=\"nc\">MyStorage</span><span class=\"p\">(</span>Storage<span class=\"p\">):</span>\n    <span class=\"o\">...</span>\n</code></pre></div>\n</li>\n<li><p>Το Django πρέπει να είναι σε θέση να αρχικοποιεί το δικό σας σύστημα αποθήκευσης χωρίς arguments. Αυτό σημαίνει ότι τυχόν ρυθμίσεις του δικού σας συστήματος θα πρέπει να λαμβάνονται από το γενικό αρχείο ρυθμίσεων <code class=\"docutils literal notranslate\">django.conf.settings</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=\"nn\">django.conf</span> <span class=\"kn\">import</span> settings\n<span class=\"kn\">from</span> <span class=\"nn\">django.core.files.storage</span> <span class=\"kn\">import</span> Storage\n\n\n<span class=\"k\">class</span> <span class=\"nc\">MyStorage</span><span class=\"p\">(</span>Storage<span class=\"p\">):</span>\n    <span class=\"k\">def</span> <span class=\"fm\">__init__</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">,</span> option<span class=\"o\">=</span><span class=\"kc\">None</span><span class=\"p\">):</span>\n        <span class=\"k\">if</span> <span class=\"ow\">not</span> option<span class=\"p\">:</span>\n            option <span class=\"o\">=</span> settings<span class=\"o\">.</span>CUSTOM_STORAGE_OPTIONS\n        <span class=\"o\">...</span>\n</code></pre></div>\n</li>\n<li><p>Η κλάση αποθήκευση σας πρέπει να υλοποιεί τις μεθόδους <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\">_open()</code></a> και <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\">_save()</code></a>, μαζί με οποιεσδήποτε άλλες, απαραίτητες για τις ανάγκες σας. Για περισσότερα σχετικά με αυτές τις μεθόδους, δείτε παρακάτω.</p>\n<p>Επιπροσθέτως, αν η κλάση σας παρέχει τη δυνατότητα τοπικής αποθήκευσης των αρχείων, θα πρέπει να παρακάμπτει (override) τη μέθοδο <code class=\"docutils literal notranslate\">path()</code>.</p>\n</li>\n<li><p>Η κλάση αποθήκευσης σας θα πρέπει να είναι <a class=\"reference internal\" href=\"/el/4.2/topics/migrations/#custom-deconstruct-method\"><span class=\"std std-ref\">deconstructible</span></a> προκειμένου να γίνει serialized όταν χρησιμοποιείται πάνω σε ένα πεδίο μέσα σε κάποιο migration. Αν το πεδίο σας έχει arguments τα οποία είναι και αυτά <a class=\"reference internal\" href=\"/el/4.2/topics/migrations/#migration-serializing\"><span class=\"std std-ref\">serializable</span></a>, μπορείτε να χρησιμοποιήσετε τον decorator <code class=\"docutils literal notranslate\">django.utils.deconstruct.deconstructible</code> (αυτό χρησιμοποιεί και το Django στο σύστημα αποθήκευσης FileSystemStorage).</p></li>\n</ol>\n<p>By default, the following methods raise <code class=\"docutils literal notranslate\">NotImplementedError</code> and will\ntypically have to be overridden:</p>\n<ul class=\"simple\">\n<li><p><a class=\"reference internal\" href=\"/el/4.2/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\">Storage.delete()</code></a></p></li>\n<li><p><a class=\"reference internal\" href=\"/el/4.2/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\">Storage.exists()</code></a></p></li>\n<li><p><a class=\"reference internal\" href=\"/el/4.2/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\">Storage.listdir()</code></a></p></li>\n<li><p><a class=\"reference internal\" href=\"/el/4.2/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\">Storage.size()</code></a></p></li>\n<li><p><a class=\"reference internal\" href=\"/el/4.2/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\">Storage.url()</code></a></p></li>\n</ul>\n<p>Υπόψιν ότι δεν είναι απαραίτητο, όλες αυτές οι μέθοδοι, να υλοποιηθούν και μερικές μπορούν σκοπίμως να παραλειφθούν. Παραβλέποντας, λοιπόν, μερικές από αυτές το σύστημα αποθήκευσης σας θα μπορεί να λειτουργεί.</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\">Storage.listdir()</code>.</p>\n<p>Άλλο παράδειγμα είναι ένα άλλο σύστημα αποθήκευσης που μοναδικός του ρόλος είναι η εγγραφή σε αρχεία. Σε αυτή την περίπτωση δεν χρειάζεται να υλοποιήσετε καμία από τις παραπάνω μεθόδους.</p>\n<p>Με άλλα λόγια, το ποιες μέθοδοι θα υλοποιηθούν εξαρτάται από σας και τις ανάγκες σας. Από την άλλη, αν μερικές δεν υλοποιηθούν θα καταλήξετε με ένα λειψό (πιθανόν χαλασμένο) interface.</p>\n<p>Επίσης, ίσως να θέλατε να χρησιμοποιήσετε κάποια hooks τα οποία έχουν σχεδιαστεί για παραμετροποιήσιμα συστήματα αποθήκευσης. Αυτά είναι:</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\">_open</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">name</em>, <em class=\"sig-param\">mode<span class=\"o\">=</span><span class=\"default_value\">'rb'</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>Υποχρεωτικό</p>\n<p>Called by <code class=\"docutils literal notranslate\">Storage.open()</code>, this is the actual mechanism the storage class\nuses to open the file. This must return a <code class=\"docutils literal notranslate\">File</code> object, though in most cases,\nyou’ll want to return some subclass here that implements logic specific to the\nbackend storage system. The <a class=\"reference external\" href=\"https://docs.python.org/3/library/exceptions.html#FileNotFoundError\" title=\"(στη Python έκδοση 3.14)\"><code class=\"xref py py-exc docutils literal notranslate\">FileNotFoundError</code></a> exception should be raised\nwhen a file doesn’t exist.</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\">_save</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">name</em>, <em class=\"sig-param\">content</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>Η μέθοδος αυτή καλείται από την <code class=\"docutils literal notranslate\">Storage.save()</code>. Το όρισμα <code class=\"docutils literal notranslate\">name</code> θα έχει περάσει ήδη από την μέθοδο <code class=\"docutils literal notranslate\">get_valid_name()</code> και <code class=\"docutils literal notranslate\">get_available_name()</code> και το όρισμα <code class=\"docutils literal notranslate\">content</code> θα είναι ένα <code class=\"docutils literal notranslate\">File</code> object.</p>\n<p>Should return the actual name of the file saved (usually the <code class=\"docutils literal notranslate\">name</code> passed\nin, 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\">get_valid_name</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">name</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>Επιστρέφει το όνομα του αρχείου το οποίο θεωρείται έγκυρο για να αποθηκευτεί στη συνέχεια από το σύστημα αποθήκευσης. Το όρισμα <code class=\"docutils literal notranslate\">name</code> είναι είτε το αρχικό όνομα του αρχείου που στάλθηκε στον server είτε, αν το <code class=\"docutils literal notranslate\">upload_to</code> είναι κάποιο callable, το όνομα που επεστράφη από αυτή τη μέθοδο αφού έχει αφαιρεθεί κάθε πληροφορία σχετικά με το path του αρχείου. Μπορείτε να παρακάμψετε αυτή τη μέθοδο για να ελέγξετε το πως οι μη-τυποποιημένοι χαρακτήρες μπορούν να μετατραπούν σε έγκυρα-ασφαλή ονόματα αρχείων.</p>\n<p>Ο κώδικας του <code class=\"docutils literal notranslate\">Storage</code> διατηρεί μόνο τους αλφαριθμητικούς χαρακτήρες, τις τελείες και τις κάτω παύλες από τα αρχικά ονόματα των αρχείων (αυτά που επιλέγονται προς αποθήκευση), αφαιρώντας οποιονδήποτε άλλον χαρακτήρα.</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\">get_alternative_name</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">file_root</em>, <em class=\"sig-param\">file_ext</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<p>Returns an alternative filename based on the <code class=\"docutils literal notranslate\">file_root</code> and <code class=\"docutils literal notranslate\">file_ext</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\">get_available_name</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">name</em>, <em class=\"sig-param\">max_length<span class=\"o\">=</span><span class=\"default_value\">None</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>Επιστρέφει ένα όνομα αρχείου το οποίο είναι διαθέσιμο στο μηχανισμό αποθήκευσης και πιθανόν να λαμβάνει υπόψιν του το, περασμένο ως όρισμα, όνομα του αρχείου. Το όρισμα <code class=\"docutils literal notranslate\">name</code> θα έχει ήδη μετατραπεί σε κάποιο έγκυρο όνομα αρχείου για αποθήκευση σύμφωνα με την μέθοδο <code class=\"docutils literal notranslate\">get_valid_name()</code> που περιγράφηκε πιο πάνω.</p>\n<p>Το μήκος του ονόματος του αρχείου δεν μπορεί να υπερβαίνει το <code class=\"docutils literal notranslate\">max_length</code>, αν αυτό δίνεται. Αν ένα μοναδικό όνομα αρχείου δεν μπορεί να βρεθεί, τότε γίνεται raise το exception <a class=\"reference internal\" href=\"/el/4.2/ref/exceptions/#django.core.exceptions.SuspiciousOperation\" title=\"django.core.exceptions.SuspiciousOperation\"><code class=\"xref py py-exc docutils literal notranslate\">SuspiciousFileOperation</code></a>.</p>\n<p>If a file with <code class=\"docutils literal notranslate\">name</code> already exists, <code class=\"docutils literal notranslate\">get_alternative_name()</code> is called to\nobtain an alternative name.</p>\n<section id=\"use-your-custom-storage-engine\">\n<span id=\"using-custom-storage-engine\"></span><h2>Use your custom storage engine<a class=\"heading-anchor\" href=\"#use-your-custom-storage-engine\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<aside class=\"version-note version-added\" data-version=\"4.2\" role=\"note\">\n<p class=\"version-note-title\">New in Django 4.2</p></aside>\n<p>The first step to using your custom storage with Django is to tell Django about\nthe file storage backend you’ll be using. This is done using the\n<a class=\"reference internal\" href=\"/el/4.2/ref/settings/#std-setting-STORAGES\"><code class=\"xref std std-setting docutils literal notranslate\">STORAGES</code></a> setting. This setting maps storage aliases, which are a way\nto refer to a specific storage throughout Django, to a dictionary of settings\nfor that specific storage backend. The settings in the inner dictionaries are\ndescribed fully in the <a class=\"reference internal\" href=\"/el/4.2/ref/settings/#std-setting-STORAGES\"><code class=\"xref std std-setting docutils literal notranslate\">STORAGES</code></a> documentation.</p>\n<p>Storages are then accessed by alias from from the\n<a class=\"reference internal\" href=\"/el/4.2/ref/files/storage/#django.core.files.storage.storages\" title=\"django.core.files.storage.storages\"><code class=\"xref py py-data docutils literal notranslate\">django.core.files.storage.storages</code></a> dictionary:</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=\"nn\">django.core.files.storage</span> <span class=\"kn\">import</span> storages\n\nexample_storage <span class=\"o\">=</span> storages<span class=\"p\">[</span><span class=\"s2\">&quot;example&quot;</span><span class=\"p\">]</span>\n</code></pre></div>\n</section>","rootId":"how-to-write-a-custom-storage-class","toc":[{"title":"Use your custom storage engine","anchor":"use-your-custom-storage-engine","children":[]}],"breadcrumbs":[{"docname":"howto/index","title":"Οδηγοί «Πως-να»","url":"/el/4.2/howto/"}],"prev":{"docname":"howto/custom-template-tags","title":"How to create custom template tags and filters","url":"/el/4.2/howto/custom-template-tags/"},"next":{"docname":"howto/deployment/index","title":"How to deploy Django","url":"/el/4.2/howto/deployment/"},"formats":{"html":"/el/4.2/howto/custom-file-storage/","markdown":"/el/4.2/howto/custom-file-storage.md","json":"/el/4.2/howto/custom-file-storage.json"},"source":"https://github.com/django/django/blob/stable/4.2.x/docs/howto/custom-file-storage.txt","official":"https://docs.djangoproject.com/el/4.2/howto/custom-file-storage/","inVersions":["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"],"inLocales":["en","zh-hans","fr","ja","id","it","pt-br","ko","es","el","pl"]}