Django使用FTP处理大文件

问题描述

我正在使用Django admin将大型文件上传到另一台服务器(下载主机)。 文件通常为100mb。 我目前使用的是基于this的FTP。 它适用于小于1mb的文件,但正如其本身在文档中所说,它不适用于较大的文件,并且在Django管理员中上传完成后得到503。 我确实在搜索有关此问题的另一种方法,但是在Django中似乎没有其他方法。 你能帮我吗?

这是我的设置。py

FTP_STORAGE_LOCATION = 'ftp://<myuser>:<mypass>@<host>:<port>/[path]'

我的模型。py

from . import ftp
fs = ftp.FTPStorage()


def my_awesome_upload_function(instance,filename):
     return os.path.join('public_ftp/public/{}/'.format(instance.get_directory()),filename)

class Video(models.Model):
    video_file_ftp = models.FileField(upload_to = my_awesome_upload_function,storage=fs)

这是ftp.py

# FTP storage class for Django pluggable storage system.
# Author: Rafal Jonca <jonca.rafal@gmail.com>
# License: MIT
# Comes from http://www.djangosnippets.org/snippets/1269/
#
# Usage:
#
# Add below to settings.py:
# FTP_STORAGE_LOCATION = '[a]ftp://<user>:<pass>@<host>:<port>/[path]'
#
# In models.py you can write:
# from FTPStorage import FTPStorage
# fs = FTPStorage()
# class FTPTest(models.Model):
#     file = models.FileField(upload_to='a/b/c/',storage=fs)

import ftplib
import io
import os
from datetime import datetime
from urllib.parse import urljoin,urlparse

from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
from django.core.files.base import File
from django.core.files.storage import Storage
from django.utils.deconstruct import deconstructible

from storages.utils import setting


class FTPStorageException(Exception):
    pass


@deconstructible
class FTPStorage(Storage):
    """FTP Storage class for Django pluggable storage system."""

    def __init__(self,location=None,base_url=None,encoding=None):
        location = location or setting('FTP_STORAGE_LOCATION')
        if location is None:
            raise ImproperlyConfigured("You must set a location at "
                                       "instanciation or at "
                                       " settings.FTP_STORAGE_LOCATION'.")
        self.location = location
        self.encoding = encoding or setting('FTP_STORAGE_ENCODING') or 'latin-1'
        base_url = base_url or settings.MEDIA_URL
        self._config = self._decode_location(location)
        self._base_url = base_url
        self._connection = None

    def _decode_location(self,location):
        """Return splitted configuration data from location."""
        splitted_url = urlparse(location)
        config = {}

        if splitted_url.scheme not in ('ftp','aftp'):
            raise ImproperlyConfigured(
                'FTPStorage works only with FTP protocol!'
            )
        if splitted_url.hostname == '':
            raise ImproperlyConfigured('You must at least provide hostname!')

        if splitted_url.scheme == 'aftp':
            config['active'] = True
        else:
            config['active'] = False
        config['path'] = splitted_url.path
        config['host'] = splitted_url.hostname
        config['user'] = splitted_url.username
        config['passwd'] = splitted_url.password
        config['port'] = int(splitted_url.port)

        return config

    def _start_connection(self):
        # Check if connection is still alive and if not,drop it.
        if self._connection is not None:
            try:
                self._connection.pwd()
            except ftplib.all_errors:
                self._connection = None

        # Real reconnect
        if self._connection is None:
            ftp = ftplib.FTP()
            ftp.encoding = self.encoding
            try:
                ftp.connect(self._config['host'],self._config['port'])
                ftp.login(self._config['user'],self._config['passwd'])
                if self._config['active']:
                    ftp.set_pasv(False)
                if self._config['path'] != '':
                    ftp.cwd(self._config['path'])
                self._connection = ftp
                return
            except ftplib.all_errors:
                raise FTPStorageException(
                    'Connection or login error using data %s'
                    % repr(self._config)
                )

    def disconnect(self):
        self._connection.quit()
        self._connection = None

    def _mkremdirs(self,path):
        pwd = self._connection.pwd()
        path_splitted = path.split(os.path.sep)
        for path_part in path_splitted:
            try:
                self._connection.cwd(path_part)
            except ftplib.all_errors:
                try:
                    self._connection.mkd(path_part)
                    self._connection.cwd(path_part)
                except ftplib.all_errors:
                    raise FTPStorageException(
                        'Cannot create directory chain %s' % path
                    )
        self._connection.cwd(pwd)
        return

    def _put_file(self,name,content):
        # Connection must be open!
        try:
            self._mkremdirs(os.path.dirname(name))
            pwd = self._connection.pwd()
            self._connection.cwd(os.path.dirname(name))
            self._connection.storbinary('STOR ' + os.path.basename(name),content.file,content.DEFAULT_CHUNK_SIZE)
            self._connection.cwd(pwd)
        except ftplib.all_errors:
            raise FTPStorageException('Error writing file %s' % name)

    def _open(self,mode='rb'):
        remote_file = FTPStorageFile(name,self,mode=mode)
        return remote_file

    def _read(self,name):
        memory_file = io.BytesIO()
        try:
            pwd = self._connection.pwd()
            self._connection.cwd(os.path.dirname(name))
            self._connection.retrbinary('RETR ' + os.path.basename(name),memory_file.write)
            self._connection.cwd(pwd)
            memory_file.seek(0)
            return memory_file
        except ftplib.all_errors:
            raise FTPStorageException('Error reading file %s' % name)

    def _save(self,content):
        content.open()
        self._start_connection()
        self._put_file(name,content)
        content.close()
        return name

    def _get_dir_details(self,path):
        # Connection must be open!
        try:
            lines = []
            self._connection.retrlines('LIST ' + path,lines.append)
            dirs = {}
            files = {}
            for line in lines:
                words = line.split()
                if len(words) < 6:
                    continue
                if words[-2] == '->':
                    continue
                if words[0][0] == 'd':
                    dirs[words[-1]] = 0
                elif words[0][0] == '-':
                    files[words[-1]] = int(words[-5])
            return dirs,files
        except ftplib.all_errors:
            raise FTPStorageException('Error getting listing for %s' % path)

    def modified_time(self,name):
        self._start_connection()
        resp = self._connection.sendcmd('MDTM ' + name)
        if resp[:3] == '213':
            s = resp[3:].strip()
            # workaround for broken FTP servers returning responses
            # starting with e.g. 1904... instead of 2004...
            if len(s) == 15 and s[:2] == '19':
                s = str(1900 + int(s[2:5])) + s[5:]
            return datetime.strptime(s,'%Y%m%d%H%M%S')
        raise FTPStorageException(
                'Error getting modification time of file %s' % name
        )

    def listdir(self,path):
        self._start_connection()
        try:
            dirs,files = self._get_dir_details(path)
            return list(dirs.keys()),list(files.keys())
        except FTPStorageException:
            raise

    def delete(self,name):
        if not self.exists(name):
            return
        self._start_connection()
        try:
            self._connection.delete(name)
        except ftplib.all_errors:
            raise FTPStorageException('Error when removing %s' % name)

    def exists(self,name):
        self._start_connection()
        try:
            nlst = self._connection.nlst(
                os.path.dirname(name) + '/'
            )
            if name in nlst or os.path.basename(name) in nlst:
                return True
            else:
                return False
        except ftplib.error_temp:
            return False
        except ftplib.error_perm:
            # error_perm: 550 Can't find file
            return False
        except ftplib.all_errors:
            raise FTPStorageException('Error when testing existence of %s'
                                      % name)

    def size(self,name):
        self._start_connection()
        try:
            dirs,files = self._get_dir_details(os.path.dirname(name))
            if os.path.basename(name) in files:
                return files[os.path.basename(name)]
            else:
                return 0
        except FTPStorageException:
            return 0

    def url(self,name):
        if self._base_url is None:
            raise ValueError("This file is not accessible via a URL.")
        return urljoin(self._base_url,name).replace('\\','/')


class FTPStorageFile(File):
    def __init__(self,storage,mode):
        self.name = name
        self._storage = storage
        self._mode = mode
        self._is_dirty = False
        self.file = io.BytesIO()
        self._is_read = False

    @property
    def size(self):
        if not hasattr(self,'_size'):
            self._size = self._storage.size(self.name)
        return self._size

    def readlines(self):
        if not self._is_read:
            self._storage._start_connection()
            self.file = self._storage._read(self.name)
            self._is_read = True
        return self.file.readlines()

    def read(self,num_bytes=None):
        if not self._is_read:
            self._storage._start_connection()
            self.file = self._storage._read(self.name)
            self._is_read = True
        return self.file.read(num_bytes)

    def write(self,content):
        if 'w' not in self._mode:
            raise AttributeError("File was opened for read-only access.")
        self.file = io.BytesIO(content)
        self._is_dirty = True
        self._is_read = True

    def close(self):
        if self._is_dirty:
            self._storage._start_connection()
            self._storage._put_file(self.name,self)
            self._storage.disconnect()
        self.file.close()

当我尝试上传大文件时,我只是在浏览器上无法获得503服务。但是文件少于1mb的话,一切正常。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...