最新消息:VPS服务器又从fzhost.net换回Linode了,主题仍用朋友推荐的大前端D8

【已解决】Flask中Mongo的GridFS数据如何保存为绝对路径的下载文件地址

Flask crifan 21浏览 0评论

折腾:

【已解决】CentOS服务器中搭建Python的Flask的REST API

期间,再去想办法,把得到的mongo中girdfs的文件数据,想办法生成绝对的url路径

类似于:

mongo gridfs file save as url

mongo gridfs file  output  as url

mongo gridfs file export as absolute url

Streams

multer-gridfs-storage – npm

mongoexport — MongoDB Manual 3.6

mongoose – How to store urls of files/images in mongodb? – Stack Overflow

doxavore/mongo-gridfs-dump: Dump MongoDB GridFS files to the file system

mongodb gridfs to flat files – Stack Overflow

mongodump — MongoDB Manual 3.6

Export files from MongoDB GridFS with directory paths

好像没有直接这种做法的

貌似是需要:

把二进制文件数据另存为文件,放到临时文件夹中,生成一个路径,供外部访问?

mongo gridfs 文件 外部绝对路径

GridFS插件 — uWSGI 2.0 文档

uWSGI支持单独的生成gridfs文件的服务器?

GridFS文件操作 · xbwen/bugu-mongo Wiki

Flask mongo gridfs server

Serve GridFs files from mongo engine with flask

Upload and Download file from Mongo using Bottle and Flask

可以参考:

<code>@FILE_API.get('/download/&lt; file_name&gt;')
def index(file_name):  
    grid_fs_file = GRID_FS.find_one({'filename': file_name})
    response.headers['Content-Type'] = 'application/octet-stream'
    response.headers["Content-Disposition"] = "attachment; filename={}".format(file_name)
    return grid_fs_file
</code>

去实现自己此处的:

外部访问:

http://ip/gridfs/file_id

然后内部就返回文件的二进制数据?

去试试

然后就需要去:

【已解决】Flask的Flask-Restful中如何获得多个GET的参数且支持可选参数

接着就是去,返回文件的二进制数据了

flask response file binary data

Flask serving binary data example

python – flask return image created from database – Stack Overflow

去试试mongo获取数据再用send_file返回

Serve GridFs files from mongo engine with flask

【总结】

然后用代码:

<code>from bson.objectid import ObjectId
from flask import send_file
import io
import re


class GridfsAPI(Resource):

    def get(self, fileId, fileName=None):
        log.info("fileId=%s, file_name=%s", fileId, fileName)

        fileIdObj = ObjectId(fileId)
        log.info("fileIdObj=%s", fileIdObj)
        if not fsCollection.exists({"_id": fileIdObj}):
            respDict = {
                "code": 404,
                "message": "Can not find file from object id %s" % (fileId),
                "data": {}
            }
            return jsonify(respDict)

        fileObj = fsCollection.get(fileIdObj)
        log.info("fileObj=%s, filename=%s, chunkSize=%s, length=%s, contentType=%s",
                 fileObj, fileObj.filename, fileObj.chunk_size, fileObj.length, fileObj.content_type)

        fileBytes = fileObj.read()
        log.info("len(fileBytes)=%s", len(fileBytes))

        outputFilename = fileObj.filename
        if fileName:
            outputFilename = fileName
        log.info("outputFilename=%s", outputFilename)

        return send_file(
            io.BytesIO(fileBytes),
            mimetype=fileObj.content_type,
            as_attachment=True,
            attachment_filename=outputFilename
        )


api.add_resource(GridfsAPI, '/gridfs/&lt;fileId&gt;', '/gridfs/&lt;fileId&gt;/&lt;fileName&gt;', endpoint='gridfs')

</code>

可以实现文件下载了。

对于Mongo中gridfs中的文件:

去PyCharm中Rest Client中测试访问:

http://localhost:5000/gridfs/5ad43ae8a4bc71fa65bae5ad/Merry Christmas, Mom and Dad.mp3

直接返回二进制数据:

从开头的ID3可以看出是mp3

而去浏览器中访问地址:

http://127.0.0.1:5000/gridfs/5ad43ae8a4bc71fa65bae5ad/Merry%20Christmas,%20Mom%20and%20Dad.mp3

则可以直接下载mp3到本地:

所以,此处可以通过:

新增gridfs的get接口,允许访问地址:

或:

http://127.0.0.1:5000/gridfs/5ad43ae8a4bc71fa65bae5ad

而直接获得下载到对应的文件

再去试试访问一个pdf文件试试

5ad43c4ea4bc71fa65baf82b

application/pdf

The Fox and the Crow.pdf

http://127.0.0.1:5000/gridfs/5ad43c4ea4bc71fa65baf82b

http://127.0.0.1:5000/gridfs/5ad43c4ea4bc71fa65baf82b/my custom name.pdf

-》

http://127.0.0.1:5000/gridfs/5ad43c4ea4bc71fa65baf82b/my%20custom%20name.pdf

也是可以下载到的:

转载请注明:在路上 » 【已解决】Flask中Mongo的GridFS数据如何保存为绝对路径的下载文件地址

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
16 queries in 0.227 seconds, using 10.36MB memory