如果你需要提供一个自定义的文件存储-一个常见的例子便是在远程系统上存储文件-你可以通过定义一个自己的存储类来做这件事情,你将通过一下步骤:

  • 你自定义的存储系统一定是django.core.files.storage.Storage的子类
from django.core.files.storage import Storage

class MyStorage(Storage):
...
  • django必须可以实例化你的存储系统(不使用任何参数),这意味着任何的设置都必须从django.conf.settings中拿
from django.conf import settings
from django.core.files.storage import Storage class MyStorage(Storage):
def __init__(self, option=None):
if not option:
option = settings.CUSTOM_STORAGE_OPTIONS
  • 你的存储类必须实现_open()和_save()方法,以及其他的和你存储类相关的方法,下面会讲到
  • 另外,如果你的类提供本地文件存储,你必须覆盖path()方法,如果不,可以忽略这个方法

这是基类Storage的源码,我会在源码的注释中讲解一些内容一些要注意的内容

class Storage(object):
""" 存储基类,提供一些默认的行为供其他的存储系统继承或者覆盖(如果需要的话) """
# 下面的方法代表了私有方法的一个公共接口,除非绝对的需要,这些方法不应该被子类覆盖
def open(self, name, mode='rb'):
""" 从存储中检索特定的文件 """
return self._open(name, mode) def save(self, name, content):
""" 用给定的文件名保存给定的新内容,内容应该是一个合适的可以从头开始读取的File对象 """
# Get the proper name for the file, as it will actually be saved.
if name is None:
name = content.name name = self.get_available_name(name)
name = self._save(name, content) # Store filenames with forward slashes, even on Windows
return force_unicode(name.replace('\\', '/')) # 这些方法是一部分的公共API(已经实现好的,当然,你可以覆盖) def get_valid_name(self, name):
""" Returns a filename, based on the provided filename, that's suitable for
use in the target storage system. """
return get_valid_filename(name) def get_available_name(self, name):
""" Returns a filename that's free on the target storage system, and
available for new content to be written to. """
dir_name, file_name = os.path.split(name)
file_root, file_ext = os.path.splitext(file_name)
# If the filename already exists, add an underscore and a number (before
# the file extension, if one exists) to the filename until the generated
# filename doesn't exist.
count = itertools.count(1)
while self.exists(name):
# file_ext includes the dot.
name = os.path.join(dir_name, "%s_%s%s" % (file_root, count.next(), file_ext)) return name def path(self, name):
"""
Returns a local filesystem path where the file can be retrieved using
Python's built-in open() function. Storage systems that can't be
accessed using open() should *not* implement this method.
"""
raise NotImplementedError("This backend doesn't support absolute paths.") # 下面的这些方法是没有提供默认实现的公共API,子类一定要实现这些方法 def delete(self, name):
""" Deletes the specified file from the storage system. """
raise NotImplementedError() def exists(self, name):
""" Returns True if a file referened by the given name already exists in the
storage system, or False if the name is available for a new file. """
raise NotImplementedError() def listdir(self, path):
""" Lists the contents of the specified path, returning a 2-tuple of lists;
the first item being directories, the second item being files. """
raise NotImplementedError() def size(self, name):
""" Returns the total size, in bytes, of the file specified by name. """
raise NotImplementedError() def url(self, name):
""" Returns an absolute URL where the file's contents can be accessed
directly by a Web browser. """
raise NotImplementedError() def accessed_time(self, name):
""" Returns the last accessed time (as datetime object) of the file
specified by name. """
raise NotImplementedError() def created_time(self, name):
""" Returns the creation time (as datetime object) of the file
specified by name. """
raise NotImplementedError() def modified_time(self, name):
""" Returns the last modified time (as datetime object) of the file
specified by name. """
raise NotImplementedError()

看完这个源码,相信你已经知道该如何如写一个自己的存储系统类了(那些方法一定要有的,那些是可以直接用的,那些是一定要写的),下面我们看一下django自带的一个实现吧

class FileSystemStorage(Storage):
""" Standard filesystem storage """
def __init__(self, location=None, base_url=None):
if location is None:
location = settings.MEDIA_ROOT
self.base_location = location
self.location = abspathu(self.base_location)
if base_url is None:
base_url = settings.MEDIA_URL
self.base_url = base_url def _open(self, name, mode='rb'):
return File(open(self.path(name), mode)) def _save(self, name, content):
full_path = self.path(name) # Create any intermediate directories that do not exist.
# Note that there is a race between os.path.exists and os.makedirs:
# if os.makedirs fails with EEXIST, the directory was created
# concurrently, and we can continue normally. Refs #16082.
directory = os.path.dirname(full_path)
if not os.path.exists(directory):
try:
os.makedirs(directory)
except OSError, e:
if e.errno != errno.EEXIST:
raise
if not os.path.isdir(directory):
raise IOError("%s exists and is not a directory." % directory) # There's a potential race condition between get_available_name and
# saving the file; it's possible that two threads might return the
# same name, at which point all sorts of fun happens. So we need to
# try to create the file, but if it already exists we have to go back
# to get_available_name() and try again. while True:
try:
# This file has a file path that we can move.
if hasattr(content, 'temporary_file_path'):
file_move_safe(content.temporary_file_path(), full_path)
content.close() # This is a normal uploadedfile that we can stream.
else:
# This fun binary flag incantation makes os.open throw an
# OSError if the file already exists before we open it.
fd = os.open(full_path, os.O_WRONLY | os.O_CREAT | os.O_EXCL | getattr(os, 'O_BINARY', 0))
try:
locks.lock(fd, locks.LOCK_EX)
for chunk in content.chunks():
os.write(fd, chunk)
finally:
locks.unlock(fd)
os.close(fd)
except OSError, e:
if e.errno == errno.EEXIST:
# Ooops, the file exists. We need a new file name.
name = self.get_available_name(name)
full_path = self.path(name)
else:
raise
else:
# OK, the file save worked. Break out of the loop.
break if settings.FILE_UPLOAD_PERMISSIONS is not None:
os.chmod(full_path, settings.FILE_UPLOAD_PERMISSIONS) return name def delete(self, name):
name = self.path(name)
# If the file exists, delete it from the filesystem.
# Note that there is a race between os.path.exists and os.remove:
# if os.remove fails with ENOENT, the file was removed
# concurrently, and we can continue normally.
if os.path.exists(name):
try:
os.remove(name)
except OSError, e:
if e.errno != errno.ENOENT:
raise def exists(self, name):
return os.path.exists(self.path(name)) def listdir(self, path):
path = self.path(path)
directories, files = [], []
for entry in os.listdir(path):
if os.path.isdir(os.path.join(path, entry)):
directories.append(entry)
else:
files.append(entry)
return directories, files def path(self, name):
try:
path = safe_join(self.location, name)
except ValueError:
raise SuspiciousOperation("Attempted access to '%s' denied." % name)
return os.path.normpath(path) def size(self, name):
return os.path.getsize(self.path(name)) def url(self, name):
if self.base_url is None:
raise ValueError("This file is not accessible via a URL.")
return urlparse.urljoin(self.base_url, filepath_to_uri(name)) def accessed_time(self, name):
return datetime.fromtimestamp(os.path.getatime(self.path(name))) def created_time(self, name):
return datetime.fromtimestamp(os.path.getctime(self.path(name))) def modified_time(self, name):
return datetime.fromtimestamp(os.path.getmtime(self.path(name)))

django “如何”系列5:如何编写自定义存储系统的更多相关文章

  1. Django学习系列4:编写第一个简单的应用代码

    首页视图编写 lists/tests.py from django.test import TestCasefrom django.urls import resolvefrom lists.view ...

  2. Win系列:VC++编写自定义组件

    在Visual Studio 中新建一个Visual C++的 Windows应用商店的Windows运行时组件项目,并将项目命名为FilePickerComponent.然后在项目的解决方案资源管理 ...

  3. django “如何”系列4:如何编写自定义模板标签和过滤器

    django的模板系统自带了一系列的内建标签和过滤器,一般情况下可以满足你的要求,如果觉得需更精准的模板标签或者过滤器,你可以自己编写模板标签和过滤器,然后使用{% load %}标签使用他们. 代码 ...

  4. Django 编写自定义的 404 / 500 报错界面

    Django 编写自定义的 404 / 500 报错界面 1. 首先 setting.py 文件中的 debug 参数设置成 false ,不启用调试. DEBUG = False 2. 在 temp ...

  5. Django编写自定义manage.py 命令

    官网文档地址:编写自定义 django-admin 命令 金句: 你所浪费的今天,正是昨天死的人所期待的明天. 开篇话: python manage.py <command> 的命令我们用 ...

  6. Wix打包系列(三)自定义Action(Custom Action)

    原文:Wix打包系列(三)自定义Action(Custom Action) 3.1 关于Action 我们已经知道如何生成具有标准安装界面的安装程序了,Windows Installer按照我们的界面 ...

  7. 《C#微信开发系列(2)-自定义菜单管理》

    2.0自定义菜单管理 ①接口说明 微信服务号聊天窗口下面的菜单项(有的公众号有启用有的则没有),这个可以在编辑模式简单配置,也可以在开发模式代码配置.微信公众平台开发者文档:微信公众号开发平台创建自定 ...

  8. python Django教程 之 模型(数据库)、自定义Field、数据表更改、QuerySet API

    python  Django教程  之 模型(数据库).自定义Field.数据表更改.QuerySet API 一.Django 模型(数据库) Django 模型是与数据库相关的,与数据库相关的代码 ...

  9. 用mel编写自定义节点的属性编辑器界面

    用mel编写自定义节点的属性编辑器界面比较麻烦,而且网上例子又少,下面给出一个范例,说明基本的格式 // 初始化节点时调用 global proc initControl(string $attrNa ...

随机推荐

  1. POJ1006:Biorhythms——题解

    http://poj.org/problem?id=1006 题目大意: 人生来就有三个生理周期,分别为体力.感情和智力周期,它们的周期长度为23天.28天和33天.每一个周期中有一天是高峰.在高峰这 ...

  2. [Leetcode] minimum window substring 最小字符窗口

    Given a string S and a string T, find the minimum window in S which will contain all the characters ...

  3. Stars POJ - 2352

    Astronomers often examine star maps where stars are represented by points on a plane and each star h ...

  4. zigbee ------ JN5169低功耗设置

    低功耗睡眠设置Power Manager (PWRM) PWRM_vInit() 如果进入睡眠模式,设置芯片进入何种睡眠模式 PWRM_eScheduleActivity()设置进入睡眠多长时间(时钟 ...

  5. linux下bash脚本语法

    1.shell中的变量定义和引用(1)变量定义和初始化.shell是弱类型语言(语言中的变量如果有明确的类型则属于强类型语言:变量没有明确类型就是弱类型语言),和C语言不同.在shell编程中定义变量 ...

  6. c# 深拷贝与浅拷贝的区别分析及实例

    浅拷贝(影子克隆):只复制对象的基本类型,对象类型,仍属于原来的引用. 深拷贝(深度克隆):不紧复制对象的基本类,同时也复制原对象中的对象.就是说完全是新对象产生的. 深拷贝是指源对象与拷贝对象互相独 ...

  7. 使用localhost调试本地代码,setcookie无效

    今天在本地调试代码的时候,再域名中使用localhost,结果一直调试不成功,最后发现在登录时,setcookie()没有设置进去 于是发现了,在使用localhost调试时,保存cookie是无效的 ...

  8. LightOJ 1341 - Aladdin and the Flying Carpet 基本因子分解

    http://www.lightoj.com/volume_showproblem.php?problem=1341 题意:给你长方形的面积a,边最小为b,问有几种情况. 思路:对a进行素因子分解,再 ...

  9. 15、简述MySQL的执行计划?

    具体的Mysql的执行计划,请参考下面的链接: MySQL_执行计划详细说明

  10. Android Studio注意事项

    http://www.android-studio.org/ 解决方法: 在 Android Studio 安装目录 bin/idea.properties 文件最后追加一句 1 disable.an ...