GridFS存储文件
MongoDB针对于文件,尤其是大文件,专门设计了一套接口,叫做:GridFS
此处总结一下GridFS的使用心得。
TODO:
- 如何基于Flask搭建一个文件下载服务,甚至可以支持断点续传功能。把之前相关代码贴过来。
- 把更多帖子内容整理至此
- GridFS
- 【已解决】GridFS保存文件时如何得到文件的id或_id
- 【已解决】用Python去连接本地mongoDB去用GridFS保存文件
- 【已解决】MongoDB的GridFS中基于文件名或id去下载文件
- 【已解决】MongoDB的GridFS中只返回file的chunks的个数而不返回chunks.data
- 【基本解决】Python中把wma、wav等格式音频转换为mp3
- 【未解决】Mongo中更新gridfs中的mp3文件的metadata信息且尽量保持id不变
- 【已解决】远程的mongoDB中GridFS报错:AttributeError GridFS object has no attribute totalSize
- 【规避解决】Flask-PyMongo中如何查询gridfs中的文件
- 【已解决】如何用PyMongo中的GridFS的put去保存添加文件
- 【已解决】PyMongo中GridFS的exists始终检测不到文件已存在
- 【已解决】Flask中Mongo的GridFS数据如何保存为绝对路径的下载文件地址
- 【已解决】MongoDB通过GridFS的API的put保存文件时添加额外信息
- 【已解决】PyMongo的GridFS中使用fs的collection去put出错:TypeError Collection object is not callable
- 【已解决】把本地mp3文件存入在线Mongo中且填写meta信息
- 【已解决】Flask中连接远程MongoDB数据库的gridfs并返回查询到的文件数据
- 和其中mongofiles相关的:
- GridFS
一些供参考的资料:
- GirdFS官网文档
- Pymongo
一些GirdFS的心得
通过id查找gridfs中file文件
注意不是:
> db.fs.files.find({"id": "5b556ee47f4d38ba6a189222"})
> db.fs.files.find({"_id": "5b556ee47f4d38ba6a189222"})
而是ObjectId("5b556ee47f4d38ba6a189222")
:
> db.fs.files.find({"_id": ObjectId("5b556ee47f4d38ba6a189222")})
{ "_id" : ObjectId("5b556ee47f4d38ba6a189222"), "contentType" : "audio/mpeg", "chunkSize" : 261120, "metadata" : { "song" : { "singers" : [ "ChuChu TV" ] }, "series" : { "number" : 0, "name" : "" }, "topics" : [ ], "storybook" : { "publisher" : "", "isFiction" : "未知", "lexileIndex" : "", "awards" : "", "authors" : [ ], "foreignCountry" : "" }, "keywords" : { "fromName" : [ "Finger", "Family" ], "other" : [ ], "fromContent" : [ "Daddy Finger" ] }, "name" : "Finger Family", "resourceType" : "song", "mainActors" : [ ], "contentAbstract" : "", "isSeries" : false, "fitAgeStart" : 3, "fitAgeEnd" : 6, "fileInfo" : { "isAudio" : true, "contentType" : "audio/mpeg", "name" : "Finger Family.mp3", "suffix" : "mp3" } }, "filename" : "Finger Family.mp3", "length" : 2604280, "uploadDate" : ISODate("2018-07-23T06:00:04.925Z"), "md5" : "946564effc4c0a835c55564377e7d819" }
>
删除文件
举例:
> db.fs.files.deleteOne({"_id": ObjectId("5b556ee47f4d38ba6a189222")})
{ "acknowledged" : true, "deletedCount" : 1 }
格式化带缩进美观的输出结果
后面加上:.pretty()
> db.fs.files.find({"_id": ObjectId("5b556ee47f4d38ba6a189222")}).pretty()
{
"_id" : ObjectId("5b556ee47f4d38ba6a189222"),
"contentType" : "audio/mpeg",
"chunkSize" : 261120,
"metadata" : {
"song" : {
"singers" : [
"ChuChu TV"
]
},
"series" : {
"number" : 0,
"name" : ""
},
"topics" : [ ],
"storybook" : {
"publisher" : "",
"isFiction" : "未知",
"lexileIndex" : "",
"awards" : "",
"authors" : [ ],
"foreignCountry" : ""
},
"keywords" : {
"fromName" : [
"Finger",
"Family"
],
"other" : [ ],
"fromContent" : [
"Daddy Finger"
]
},
"name" : "Finger Family",
"resourceType" : "song",
"mainActors" : [ ],
"contentAbstract" : "",
"isSeries" : false,
"fitAgeStart" : 3,
"fitAgeEnd" : 6,
"fileInfo" : {
"isAudio" : true,
"contentType" : "audio/mpeg",
"name" : "Finger Family.mp3",
"suffix" : "mp3"
}
},
"filename" : "Finger Family.mp3",
"length" : 2604280,
"uploadDate" : ISODate("2018-07-23T06:00:04.925Z"),
"md5" : "946564effc4c0a835c55564377e7d819"
}
>