Flask / MongoDB 搭建简易图片服务器
前期准备 通过 pip 或 easy_install 安装了 pymongo 之后, 就能通过 Python 调教 mongodb 了. 接着安装个 flask 用来当 web 服务器. 当然 mongo 也是得安装的. 对于 Ubuntu 用户, 特别是使用 Server 12.04 的同学, 安装最新版要略费些周折, 具体说是 sudoapt
前期准备
通过 pip 或 easy_install 安装了 pymongo 之后, 就能通过 Python 调教 mongodb 了.接着安装个 flask 用来当 web 服务器.
当然 mongo 也是得安装的. 对于 Ubuntu 用户, 特别是使用 Server 12.04 的同学, 安装最新版要略费些周折, 具体说是
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10<br>echo 'deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen' | sudo tee /etc/apt/sources.list.d/mongodb.list<br>sudo apt-get update<br>sudo apt-get install mongodb-10gen<br>
pip install Pillow<br>
easy_install Pillow<br>
正片
Flask 文件上传
Flask 官网上那个例子居然分了两截让人无从吐槽. 这里先弄个最简单的, 无论什么文件都先弄上来import flask<br><br>app = flask.Flask(__name__)<br>app.debug = True<br><br>@app.route('/upload', methods=['POST'])<br>def upload():<br> f = flask.request.files['uploaded_file']<br> print f.read()<br> return flask.redirect('/')<br><br>@app.route('/')<br>def index():<br> return '''<br> nbsp;html><br> <br> <br> <form>
<br> <input><br> <input><br> </form>
<br> '''<br><br>if __name__ == '__main__':<br> app.run(port=7777)<br>
- 注: 在
upload
函数中, 使用flask.request.files[KEY]
获取上传文件对象, KEY 为页面 form 中 input 的 name 值
保存到 mongodb
如果不那么讲究的话, 最快速基本的存储方案里只需要import pymongo<br>import bson.binary<br>from cStringIO import StringIO<br><br>app = flask.Flask(__name__)<br>app.debug = True<br><strong class="ntstrong">db = pymongo.MongoClient('localhost', 27017).test</strong><br><br>def save_file(f):<br> content = StringIO(f.read())<br> db.files.save(dict(<br> content=<strong class="ntstrong">bson.binary.Binary(content.getvalue())</strong>,<br> ))<br><br>@app.route('/upload', methods=['POST'])<br>def upload():<br> f = flask.request.files['uploaded_file']<br> <strong class="ntstrong">save_file(f)</strong><br> return flask.redirect('/')<br>
bson.binary.Binary
对象, 再把它扔进 mongodb 就可以了.现在试试再上传个什么文件, 在 mongo shell 中通过
db.files.find()<br>
content
这个域几乎肉眼无法分辨出什么东西, 即使是纯文本文件, mongo 也会显示为 Base64 编码.提供文件访问
给定存进数据库的文件的 ID (作为 URI 的一部分), 返回给浏览器其文件内容, 如下def save_file(f):<br> content = StringIO(f.read())<br> <strong class="ntstrong">c = dict(content=bson.binary.Binary(content.getvalue()))</strong><br> <strong class="ntstrong">db.files.save(c)</strong><br> <strong class="ntstrong">return c['_id']</strong><br><br>@app.route('/f/<fid>')<br><strong class="ntstrong">def serve_file(fid):</strong><br> f = db.files.find_one(bson.objectid.ObjectId(fid))<br> return f['content']<br><br>@app.route('/upload', methods=['POST'])<br>def upload():<br> f = flask.request.files['uploaded_file']<br> fid = save_file(f)<br> return flask.redirect(<strong class="ntstrong">'/f/' + str(fid)</strong>)<br></fid>
upload
函数会跳转到对应的文件浏览页. 这样一来, 文本文件内容就可以正常预览了, 如果不是那么挑剔换行符跟连续空格都被浏览器吃掉的话.当找不到文件时
有两种情况, 其一, 数据库 ID 格式就不对, 这时 pymongo 会抛异常bson.errors.InvalidId
; 其二, 找不到对象 (!), 这时 pymongo 会返回 None
.简单起见就这样处理了
@app.route('/f/<fid>')<br>def serve_file(fid):<br> import bson.errors<br> try:<br> f = db.files.find_one(bson.objectid.ObjectId(fid))<br> if f is None:<br> raise bson.errors.InvalidId()<br> return f['content']<br> except bson.errors.InvalidId:<br> flask.abort(404)<br></fid>
正确的 MIME
从现在开始要对上传的文件严格把关了, 文本文件, 狗与剪刀等皆不能上传.判断图片文件之前说了我们动真格用 Pillow
from PIL import Image<br><br>allow_formats = set(['jpeg', 'png', 'gif'])<br><br>def save_file(f):<br> content = StringIO(f.read())<br> try:<br> mime = <strong class="ntstrong">Image.open(content).format.lower()</strong><br> if mime not in allow_formats:<br> raise IOError()<br> except IOError:<br> flask.abort(400)<br> c = dict(content=bson.binary.Binary(content.getvalue()))<br> db.files.save(c)<br> return c['_id']<br>
要解决这个问题, 得把 MIME 一并存到数据库里面去; 并且, 在给出文件时也正确地传输 mimetype
def save_file(f):<br> content = StringIO(f.read())<br> try:<br> mime = Image.open(content).format.lower()<br> if mime not in allow_formats:<br> raise IOError()<br> except IOError:<br> flask.abort(400)<br> c = dict(content=bson.binary.Binary(content.getvalue()), mime=mime)<br> db.files.save(c)<br> return c['_id']<br><br>@app.route('/f/<fid>')<br>def serve_file(fid):<br> try:<br> f = db.files.find_one(bson.objectid.ObjectId(fid))<br> if f is None:<br> raise bson.errors.InvalidId()<br> return <strong class="ntstrong">flask.Response(f['content'], mimetype='image/' + f['mime'])</strong><br> except bson.errors.InvalidId:<br> flask.abort(404)<br></fid>
db.files.drop()
清掉原来的数据.根据上传时间给出 NOT MODIFIED
利用 HTTP 304 NOT MODIFIED 可以尽可能压榨与利用浏览器缓存和节省带宽. 这需要三个操作- 记录文件最后上传的时间
- 当浏览器请求这个文件时, 向请求头里塞一个时间戳字符串
- 当浏览器请求文件时, 从请求头中尝试获取这个时间戳, 如果与文件的时间戳一致, 就直接 304
import datetime<br><br>def save_file(f):<br> content = StringIO(f.read())<br> try:<br> mime = Image.open(content).format.lower()<br> if mime not in allow_formats:<br> raise IOError()<br> except IOError:<br> flask.abort(400)<br> c = dict(<br> content=bson.binary.Binary(content.getvalue()),<br> mime=mime,<br> <strong class="ntstrong">time=datetime.datetime.utcnow()</strong>,<br> )<br> db.files.save(c)<br> return c['_id']<br><br>@app.route('/f/<fid>')<br>def serve_file(fid):<br> try:<br> f = db.files.find_one(bson.objectid.ObjectId(fid))<br> if f is None:<br> raise bson.errors.InvalidId()<br> if <strong class="ntstrong">flask.request.headers.get('If-Modified-Since') == f['time'].ctime()</strong>:<br> return <strong class="ntstrong">flask.Response(status=304)</strong><br> resp = flask.Response(f['content'], mimetype='image/' + f['mime'])<br> <strong class="ntstrong">resp.headers['Last-Modified'] = f['time'].ctime()</strong><br> return resp<br> except bson.errors.InvalidId:<br> flask.abort(404)<br></fid>
顺带吐个槽, 其实 NoSQL DB 在这种环境下根本体现不出任何优势, 用起来跟 RDB 几乎没两样.
利用 SHA-1 排重
与冰箱里的可乐不同, 大部分情况下你肯定不希望数据库里面出现一大波完全一样的图片. 图片, 连同其 EXIFF 之类的数据信息, 在数据库中应该是惟一的, 这时使用略强一点的散列技术来检测是再合适不过了.达到这个目的最简单的就是建立一个 SHA-1 惟一索引, 这样数据库就会阻止相同的东西被放进去.
在 MongoDB 中表中建立惟一索引, 执行 (Mongo 控制台中)
db.files.ensureIndex({sha1: 1}, {unique: true})<br>
解决方案有三个
- 删掉现在所有的数据 (一定是测试数据库才用这种不负责任的方式吧!)
- 建立一个 sparse 索引, 这个索引不要求幽灵属性惟一, 不过出现多个 null 值还是会判定重复 (不管现有数据的话可以这么搞)
- 写个脚本跑一次数据库, 把所有已经存入的数据翻出来, 重新计算 SHA-1, 再存进去
import hashlib<br><br>def save_file(f):<br> content = StringIO(f.read())<br> try:<br> mime = Image.open(content).format.lower()<br> if mime not in allow_formats:<br> raise IOError()<br> except IOError:<br> flask.abort(400)<br><br> <strong class="ntstrong">sha1 = hashlib.sha1(content.getvalue()).hexdigest()</strong><br> c = dict(<br> content=bson.binary.Binary(content.getvalue()),<br> mime=mime,<br> time=datetime.datetime.utcnow(),<br> <strong class="ntstrong">sha1=sha1</strong>,<br> )<br> <strong class="ntstrong">try:</strong><br> db.files.save(c)<br> <strong class="ntstrong">except pymongo.errors.DuplicateKeyError:</strong><br> pass<br> return c['_id']<br>
c['_id']
将会是一个不存在的数据 ID. 修正这个问题, 最好是返回 sha1
, 另外, 在访问文件时, 相应地修改为用文件 SHA-1 访问, 而不是用 ID.最后修改的结果及本篇完整源代码如下
import hashlib<br>import datetime<br>import flask<br>import pymongo<br>import bson.binary<br>import bson.objectid<br>import bson.errors<br>from cStringIO import StringIO<br>from PIL import Image<br><br>app = flask.Flask(__name__)<br>app.debug = True<br>db = pymongo.MongoClient('localhost', 27017).test<br>allow_formats = set(['jpeg', 'png', 'gif'])<br><br>def save_file(f):<br> content = StringIO(f.read())<br> try:<br> mime = Image.open(content).format.lower()<br> if mime not in allow_formats:<br> raise IOError()<br> except IOError:<br> flask.abort(400)<br><br> sha1 = hashlib.sha1(content.getvalue()).hexdigest()<br> c = dict(<br> content=bson.binary.Binary(content.getvalue()),<br> mime=mime,<br> time=datetime.datetime.utcnow(),<br> sha1=sha1,<br> )<br> try:<br> db.files.save(c)<br> except pymongo.errors.DuplicateKeyError:<br> pass<br> return sha1<br><br>@app.route('/f/<sha1>')<br>def serve_file(sha1):<br> try:<br> f = db.files.find_one({'sha1': sha1})<br> if f is None:<br> raise bson.errors.InvalidId()<br> if flask.request.headers.get('If-Modified-Since') == f['time'].ctime():<br> return flask.Response(status=304)<br> resp = flask.Response(f['content'], mimetype='image/' + f['mime'])<br> resp.headers['Last-Modified'] = f['time'].ctime()<br> return resp<br> except bson.errors.InvalidId:<br> flask.abort(404)<br><br>@app.route('/upload', methods=['POST'])<br>def upload():<br> f = flask.request.files['uploaded_file']<br> sha1 = save_file(f)<br> return flask.redirect('/f/' + str(sha1))<br><br>@app.route('/')<br>def index():<br> return '''<br> nbsp;html><br> <br> <br> <form>
<br> <input><br> <input><br> </form>
<br> '''<br><br>if __name__ == '__main__':<br> app.run(port=7777)<br></sha1>
原文地址:Flask / MongoDB 搭建简易图片服务器, 感谢原作者分享。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











This article introduces how to configure MongoDB on Debian system to achieve automatic expansion. The main steps include setting up the MongoDB replica set and disk space monitoring. 1. MongoDB installation First, make sure that MongoDB is installed on the Debian system. Install using the following command: sudoaptupdatesudoaptinstall-ymongodb-org 2. Configuring MongoDB replica set MongoDB replica set ensures high availability and data redundancy, which is the basis for achieving automatic capacity expansion. Start MongoDB service: sudosystemctlstartmongodsudosys

When developing an e-commerce website, I encountered a difficult problem: how to provide users with personalized product recommendations. Initially, I tried some simple recommendation algorithms, but the results were not ideal, and user satisfaction was also affected. In order to improve the accuracy and efficiency of the recommendation system, I decided to adopt a more professional solution. Finally, I installed andres-montanez/recommendations-bundle through Composer, which not only solved my problem, but also greatly improved the performance of the recommendation system. You can learn composer through the following address:

This article describes how to build a highly available MongoDB database on a Debian system. We will explore multiple ways to ensure data security and services continue to operate. Key strategy: ReplicaSet: ReplicaSet: Use replicasets to achieve data redundancy and automatic failover. When a master node fails, the replica set will automatically elect a new master node to ensure the continuous availability of the service. Data backup and recovery: Regularly use the mongodump command to backup the database and formulate effective recovery strategies to deal with the risk of data loss. Monitoring and Alarms: Deploy monitoring tools (such as Prometheus, Grafana) to monitor the running status of MongoDB in real time, and

It is impossible to view MongoDB password directly through Navicat because it is stored as hash values. How to retrieve lost passwords: 1. Reset passwords; 2. Check configuration files (may contain hash values); 3. Check codes (may hardcode passwords).

Detailed explanation of MongoDB efficient backup strategy under CentOS system This article will introduce in detail the various strategies for implementing MongoDB backup on CentOS system to ensure data security and business continuity. We will cover manual backups, timed backups, automated script backups, and backup methods in Docker container environments, and provide best practices for backup file management. Manual backup: Use the mongodump command to perform manual full backup, for example: mongodump-hlocalhost:27017-u username-p password-d database name-o/backup directory This command will export the data and metadata of the specified database to the specified backup directory.

Encrypting MongoDB database on a Debian system requires following the following steps: Step 1: Install MongoDB First, make sure your Debian system has MongoDB installed. If not, please refer to the official MongoDB document for installation: https://docs.mongodb.com/manual/tutorial/install-mongodb-on-debian/Step 2: Generate the encryption key file Create a file containing the encryption key and set the correct permissions: ddif=/dev/urandomof=/etc/mongodb-keyfilebs=512

GitLab Database Deployment Guide on CentOS System Selecting the right database is a key step in successfully deploying GitLab. GitLab is compatible with a variety of databases, including MySQL, PostgreSQL, and MongoDB. This article will explain in detail how to select and configure these databases. Database selection recommendation MySQL: a widely used relational database management system (RDBMS), with stable performance and suitable for most GitLab deployment scenarios. PostgreSQL: Powerful open source RDBMS, supports complex queries and advanced features, suitable for handling large data sets. MongoDB: Popular NoSQL database, good at handling sea

To set up a MongoDB user, follow these steps: 1. Connect to the server and create an administrator user. 2. Create a database to grant users access. 3. Use the createUser command to create a user and specify their role and database access rights. 4. Use the getUsers command to check the created user. 5. Optionally set other permissions or grant users permissions to a specific collection.
