我有一个基于烧瓶的网络应用程序,允许用户上传文件。文件存储在 mysql 数据库中。
这工作正常,直到文件大于大约 16Mb,插入失败并显示以下内容:
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1518, in __call__
return self.wsgi_app(environ, start_response)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1506, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1504, in wsgi_app
response = self.full_dispatch_request()
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1264, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1262, in full_dispatch_request
rv = self.dispatch_request()
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1248, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/<redacted>/access_control.py", line 15, in decorated_function
return f(*args, **kwargs)
File "/<redacted>/views/files.py", line 48, in upload
VALUES (%s, %s, %s, %s, %s)""", file_details)
File "/<redacted>/database.py", line 66, in query
cursor.execute(sql, values)
File "/usr/local/lib/python2.7/dist-packages/pymysql/cursors.py", line 262, in execute
result = super(DictCursor, self).execute(query, args)
File "/usr/local/lib/python2.7/dist-packages/pymysql/cursors.py", line 117, in execute
self.errorhandler(self, exc, value)
File "/usr/local/lib/python2.7/dist-packages/pymysql/connections.py", line 187, in defaulterrorhandler
raise Error(errorclass, errorvalue)
Error: (<class 'socket.error'>, error(32, 'Broken pipe'))
当文件大小似乎与 mySQL 设置匹配时,我很兴奋max_allowed_packet
,所以我将其更改my.cnf
并重新启动,但它没有帮助。(show variables like 'max_allowed_packet'
显示新值150M)
该文件肯定会上传到服务器,我在我的插入方法中放入了一些代码,以便在运行查询之前将文件写入磁盘并且文件没问题。
插入 blob 的字段是一个 longblob,负责插入的代码是:
@mod.route('/file/upload', methods=['POST'])
@login_required
def upload():
filename = request.files['file'].filename
mime_type = request.files['file'].mimetype
#filesize = request.files['file'].content_length
file = request.files['file'].stream.read()
if mime_type[:5] == 'image':
file = resize_image_to_width(file, 1024)
filesize = len(file)
if filesize == 0:
return ""
if not request.form['file_id']:
file_details = (filename, file, mime_type, filesize, session['user']['user_id'])
file_id = database.query("""INSERT INTO files (filename, file, mime_type, filesize, owner)
VALUES (%s, %s, %s, %s, %s)""", file_details)
else:
file_details = (filename, file, mime_type, filesize, request.form['file_id'])
file_id = database.query("""UPDATE files
SET
filename=%s,
file=%s,
mime_type=%s,
filesize=%s
WHERE file_id=%s""", file_details)
return "upload complete"
我现在有点不知所措,大约一周前我确实找到了一些建议文件需要分段插入的东西,但我现在找不到(我被我的实际工作分心了! ) 而且我不知道如何将它分块插入。
我真的很感激这方面的帮助!