pymongo use BSON type of access (picture) file

Mongodb BSON format can be used to store binary file size is less than 16M, it is suitable for storing web resources in the pictures, look at the method of recording format operation BSON

Local file write Pymongo / Pymongo write data back to a local file

"""
@File        : file_demo.py
@Description : 本地文件写入Pymongo/Pymongo数据写回本地文件
@Time        : 2020/4/7 23:20
@Author      : DexterLien
@Email       : [email protected]
@Software    : PyCharm
"""
import os

from pymongo import MongoClient

# 连接带有密码认证的Mongodb,格式如下:
# mongodb://用户名:密码@服务器地址/数据库名
con = MongoClient('mongodb://username:[email protected]:8017/dexter')

# 使用名为dexter的数据库
db = con['dexter']

# 使用名为users的collection
users = db['users']

# 本地文件以BSON二进制写入数据库集合
fpath = os.path.join(os.getcwd(), 'pic.png')
with open(fpath, 'rb') as file:
    users.insert_one({
        'name': 'pic.png',
        'data': file.read()
    })

# 数据库中的BSON数据写回文件
ret = users.find_one({'name': 'pic.png'})
with open(os.path.join(os.getcwd(), 'new.png'), 'wb') as file:
    file.write(ret.get('data'))

POST upload pictures and attachments write Mongodb return

Flask line and with just a demo, recorded about the main idea

"""
@File        : post_pic.py
@Description : POST上传的图片附件写入Mongodb与返回
@Time        : 2020/4/7 23:59
@Author      : DexterLien
@Email       : [email protected]
@Software    : PyCharm
"""
from flask import Flask, request, render_template, jsonify, make_response
from pymongo import MongoClient
from werkzeug.datastructures import FileStorage

app = Flask(__name__)
app.config['JSON_AS_ASCII'] = False


def save_to_mongo(file: FileStorage):
    """
    将post请求中的文件对象保存至Mongodb,主要是通过file.stream.read()方法直接将二进制数据读出来,给Mongodb插入用
    :param file:
    :return:
    """
    con = MongoClient('mongodb://username:[email protected]:8017/dexter')
    db = con['dexter']
    users = db['users']
    users.insert_one({
        'name': file.filename,
        'data': file.stream.read()
    })


@app.route('/', methods=['GET', 'POST'])
def index():
    if request.method == 'GET':
        return render_template('index.html')

    if request.method == 'POST':
        file = request.files.get('pic')
        save_to_mongo(file)
        return jsonify({
            'code': 0,
            'msg': '上传成功'
        })


@app.route('/pic/<filename>', methods=['GET'])
def pic(filename):
    con = MongoClient('mongodb://username:[email protected]:8017/dexter')
    db = con['dexter']
    users = db['users']
    data = users.find_one({'name': filename})['data']
    # 使用Mongodb查询到的BSON数据生成response响应,后面通过设置不同的headers实现不同响应行为
    response = make_response(data)
    # 在浏览器中直接显示图片
    response.headers.set('Content-Type', 'image/png')
    # 以文件下载方式返回
    # response.headers.set(
    #     'Content-Disposition', 'attachement', filename=f'{filename}'
    # )
    return response


if __name__ == '__main__':
    app.run(debug=True)

Upload front page

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
{# 注意:Flask中需要指定form的enctype才能将文件封装发送到post请求中 #}
<form method="post" enctype=multipart/form-data>
    <input name="pic" type="file">
    <button type="submit">上传</button>
</form>
</body>
</html>
Published 219 original articles · won praise 99 · views 490 000 +

Guess you like

Origin blog.csdn.net/lpwmm/article/details/105377303