最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】MongoDB的GridFS中基于文件名或id去下载文件

MongoDB crifan 4963浏览 0评论

折腾:

【已解决】把本地的音频字幕等数据存储到本地MongoDB数据库中

期间,已经可以用put上传/存储文件到GridFS中了。

现在接着去试试,如何下载get文件,希望基于文件名或id去下载。

mongodb gridfs 下载文件

mongodb+GridFS文件的上传下载删除 – CSDN博客

MongoDB基于GridFS实现文件的分布式存储 – CSDN博客

MongoDB基于GridFS管理文件 – 小艾.luoaz – 博客园

已有:

<code>&gt; use gridfs
switched to db gridfs
&gt; list
2018-03-28T16:18:12.812+0800 E QUERY    [thread1] ReferenceError: list is not defined :
@(shell):1:1
&gt; show collections
fs.chunks
fs.files
&gt; db.fs.files.find().pretty()
{
    "_id" : ObjectId("5abb397ca4bc71fc7d71c7bd"),
    "chunkSize" : 261120,
    "uploadDate" : ISODate("2018-03-28T06:43:08.613Z"),
    "length" : 8338105,
    "md5" : "b7660d833085e9e1a21813e4d74b0cc3",
    "filename" : "/Users/crifan/dev/dev_root/company/naturling/数据/FromMaggie/英语资源/All Aboard Reading/音频/Otto the Cat.MP3"
}
</code>

去试试

<code>➜  英语资源 mongofiles -d gridfs get "Otto the Cat.MP3"
2018-03-28T17:05:23.198+0800    connected to: localhost
2018-03-28T17:05:23.204+0800    Failed: error opening GridFS file 'Otto the Cat.MP3': not found
➜  英语资源 mongofiles -d gridfs get "/Users/crifan/dev/dev_root/company/naturling/数据/FromMaggie/英语资源/All Aboard Reading/音频/Otto the Cat.MP3"
2018-03-28T17:05:47.401+0800    connected to: localhost
finished writing to /Users/crifan/dev/dev_root/company/naturling/数据/FromMaggie/英语资源/All Aboard Reading/音频/Otto the Cat.MP3
</code>

发现:

只能用filename的全部名称才可以。

默认会写入到本地的文件夹中的:

所以再去搞清楚:

【已解决】mongofiles中put保存和get下载获取时指定文件名

在此期间,通过:

mongofiles — MongoDB Manual 3.6

mongofiles – MongoDB GridFS Utility – Linux Man Pages (1)

mongofiles — MongoDB Manual

顺带看到了一些其他有关的参数:

“–replace, -r¶

Alters the behavior of mongofiles put to replace existing GridFS objects with the specified local file, rather than adding an additional object with the same name.

In the default operation, files will not be overwritten by a mongofiles put option.”

去试试,默认的非replace效果,期望的是新增加一个文件:

<code>➜  英语资源 mongofiles -d gridfs put "/Users/crifan/dev/dev_root/company/naturling/数据/FromMaggie/英语资源/All Aboard Reading/音频/Otto the Cat.MP3"
2018-03-28T17:26:34.994+0800    connected to: localhost
added file: /Users/crifan/dev/dev_root/company/naturling/数据/FromMaggie/英语资源/All Aboard Reading/音频/Otto the Cat.MP3
</code>

另外一个终端去find看看:

<code>&gt; db.fs.files.find().pretty()
{
    "_id" : ObjectId("5abb397ca4bc71fc7d71c7bd"),
    "chunkSize" : 261120,
    "uploadDate" : ISODate("2018-03-28T06:43:08.613Z"),
    "length" : 8338105,
    "md5" : "b7660d833085e9e1a21813e4d74b0cc3",
    "filename" : "/Users/crifan/dev/dev_root/company/naturling/数据/FromMaggie/英语资源/All Aboard Reading/音频/Otto the Cat.MP3"
}
{
    "_id" : ObjectId("5abb5fcaa4bc711531cee35c"),
    "chunkSize" : 261120,
    "uploadDate" : ISODate("2018-03-28T09:26:35.121Z"),
    "length" : 8338105,
    "md5" : "b7660d833085e9e1a21813e4d74b0cc3",
    "filename" : "/Users/crifan/dev/dev_root/company/naturling/数据/FromMaggie/英语资源/All Aboard Reading/音频/Otto the Cat.MP3"
}
</code>

果然是。

然后加上–replace试试

<code>➜  英语资源 mongofiles -d gridfs put "Otto the Cat.MP3" --replace --local "/Users/crifan/dev/dev_root/company/naturling/数据/FromMaggie/英语资源/All Aboard Reading/音频/Otto the Cat.MP3"
2018-03-28T17:44:24.820+0800    connected to: localhost
2018-03-28T17:44:24.828+0800    removed all instances of 'Otto the Cat.MP3' from GridFS

added file: Otto the Cat.MP3
</code>

可以看到,显示出,删除之前已存在的文件了。

find结果也可以看到:

<code>&gt; db.fs.files.find().pretty()
{
    "_id" : ObjectId("5abb397ca4bc71fc7d71c7bd"),
    "chunkSize" : 261120,
    "uploadDate" : ISODate("2018-03-28T06:43:08.613Z"),
    "length" : 8338105,
    "md5" : "b7660d833085e9e1a21813e4d74b0cc3",
    "filename" : "/Users/crifan/dev/dev_root/company/naturling/数据/FromMaggie/英语资源/All Aboard Reading/音频/Otto the Cat.MP3"
}
{
    "_id" : ObjectId("5abb5fcaa4bc711531cee35c"),
    "chunkSize" : 261120,
    "uploadDate" : ISODate("2018-03-28T09:26:35.121Z"),
    "length" : 8338105,
    "md5" : "b7660d833085e9e1a21813e4d74b0cc3",
    "filename" : "/Users/crifan/dev/dev_root/company/naturling/数据/FromMaggie/英语资源/All Aboard Reading/音频/Otto the Cat.MP3"
}
{
    "_id" : ObjectId("5abb6194a4bc71165f9f49f5"),
    "chunkSize" : 261120,
    "uploadDate" : ISODate("2018-03-28T09:34:12.537Z"),
    "length" : 8338105,
    "md5" : "b7660d833085e9e1a21813e4d74b0cc3",
    "filename" : "Otto the Cat"
}
{
    "_id" : ObjectId("5abb63f8a4bc7117f07dcf9d"),
    "chunkSize" : 261120,
    "uploadDate" : ISODate("2018-03-28T09:44:24.915Z"),
    "length" : 8338105,
    "md5" : "b7660d833085e9e1a21813e4d74b0cc3",
    "filename" : "Otto the Cat.MP3"
}
</code>

只有一个Otto the Cat.MP3

且id也从之前的:

“_id” : ObjectId(“5abb61ffa4bc7116a6fa31d4”),

变成现在的:

“_id” : ObjectId(“5abb63f8a4bc7117f07dcf9d”),

了,是新的file了。

转载请注明:在路上 » 【已解决】MongoDB的GridFS中基于文件名或id去下载文件

发表我的评论
取消评论

表情

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

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
82 queries in 0.190 seconds, using 22.14MB memory