最新消息:20181230 VPS服务器已从Linode换到腾讯云香港,主题仍用朋友推荐的大前端D8

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

MongoDB crifan 374浏览 0评论

折腾:

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

期间,之前是:

➜  英语资源 mongofiles -d gridfs put "/Users/crifan/dev/dev_root/company/xxx/英语资源/All Aboard Reading/音频/Otto the Cat.MP3"
2018-03-28T14:43:08.463+0800    connected to: localhost
added file: /Users/crifan/dev/dev_root/company/xxx/英语资源/All Aboard Reading/音频/Otto the Cat.MP3
➜  英语资源 mongofiles -d gridfs get "/Users/crifan/dev/dev_root/company/xxx/英语资源/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/xxx/英语资源/All Aboard Reading/音频/Otto the Cat.MP3

缺点是:

没有指定文件名,导致get时下载获取出的文件,直接覆盖了之前已存在的文件

而希望搞清楚如何指定:

put时,除了文件绝对路径之外,的filename值

get时,下载出来文件,保存为的文件名

mongofiles put set filename

mongofiles — MongoDB Manual 3.6

好像和这些参数有关系:

“–local <filename>, -l <filename>¶

Specifies the local filesystem name of a file for get and put operations.

In the mongofiles put and mongofiles get commands, the required <filename> modifier refers to the name the object will have in GridFS. mongofiles assumes that this reflects the file’s name on the local file system. This setting overrides this default.”

gridfs – MongoDB – mongofiles – Stack Overflow

试了试:

➜&nbsp;&nbsp;英语资源 mongofiles -d gridfs put "Otto the Cat" --local "/Users/crifan/dev/dev_root/company/xxx/英语资源/All Aboard Reading/音频/Otto the Cat.MP3"
2018-03-28T17:34:12.429+0800&nbsp;&nbsp;&nbsp;&nbsp;connected to: localhost
added file: Otto the Cat

然后是可以的指定名字和路径的。

再去加上后缀:

➜&nbsp;&nbsp;英语资源 mongofiles -d gridfs put "Otto the Cat.MP3" --local "/Users/crifan/dev/dev_root/company/xxx/英语资源/All Aboard Reading/音 频/Otto the Cat.MP3"
2018-03-28T17:35:59.717+0800&nbsp;&nbsp;&nbsp;&nbsp;connected to: localhost
added file: Otto the Cat.MP3

可以查到:

&amp;gt; db.fs.files.find().pretty()
{
&nbsp;&nbsp;&nbsp;&nbsp;"_id" : ObjectId("5abb397ca4bc71fc7d71c7bd"),
&nbsp;&nbsp;&nbsp;&nbsp;"chunkSize" : 261120,
&nbsp;&nbsp;&nbsp;&nbsp;"uploadDate" : ISODate("2018-03-28T06:43:08.613Z"),
&nbsp;&nbsp;&nbsp;&nbsp;"length" : 8338105,
&nbsp;&nbsp;&nbsp;&nbsp;"md5" : "b7660d833085e9e1a21813e4d74b0cc3",
&nbsp;&nbsp;&nbsp;&nbsp;"filename" : "/Users/crifan/dev/dev_root/company/xxx/英语资源/All Aboard Reading/音频/Otto the Cat.MP3"
}
{
&nbsp;&nbsp;&nbsp;&nbsp;"_id" : ObjectId("5abb5fcaa4bc711531cee35c"),
&nbsp;&nbsp;&nbsp;&nbsp;"chunkSize" : 261120,
&nbsp;&nbsp;&nbsp;&nbsp;"uploadDate" : ISODate("2018-03-28T09:26:35.121Z"),
&nbsp;&nbsp;&nbsp;&nbsp;"length" : 8338105,
&nbsp;&nbsp;&nbsp;&nbsp;"md5" : "b7660d833085e9e1a21813e4d74b0cc3",
&nbsp;&nbsp;&nbsp;&nbsp;"filename" : "/Users/crifan/dev/dev_root/company/xxx/英语资源/All Aboard Reading/音频/Otto the Cat.MP3"
}
{
&nbsp;&nbsp;&nbsp;&nbsp;"_id" : ObjectId("5abb6194a4bc71165f9f49f5"),
&nbsp;&nbsp;&nbsp;&nbsp;"chunkSize" : 261120,
&nbsp;&nbsp;&nbsp;&nbsp;"uploadDate" : ISODate("2018-03-28T09:34:12.537Z"),
&nbsp;&nbsp;&nbsp;&nbsp;"length" : 8338105,
&nbsp;&nbsp;&nbsp;&nbsp;"md5" : "b7660d833085e9e1a21813e4d74b0cc3",
&nbsp;&nbsp;&nbsp;&nbsp;"filename" : "Otto the Cat"
}
{
&nbsp;&nbsp;&nbsp;&nbsp;"_id" : ObjectId("5abb61ffa4bc7116a6fa31d4"),
&nbsp;&nbsp;&nbsp;&nbsp;"chunkSize" : 261120,
&nbsp;&nbsp;&nbsp;&nbsp;"uploadDate" : ISODate("2018-03-28T09:35:59.783Z"),
&nbsp;&nbsp;&nbsp;&nbsp;"length" : 8338105,
&nbsp;&nbsp;&nbsp;&nbsp;"md5" : "b7660d833085e9e1a21813e4d74b0cc3",
&nbsp;&nbsp;&nbsp;&nbsp;"filename" : "Otto the Cat.MP3"
}

然后再去试试,get输出时,也是可以指定输出文件名的:

&nbsp;&nbsp;英语资源 mongofiles -d gridfs get "Otto the Cat.MP3" --local "downloaded Otto the Cat.MP3"
2018-03-28T17:37:43.091+0800&nbsp;&nbsp;&nbsp;&nbsp;connected to: localhost
finished writing to downloaded Otto the Cat.MP3
➜&nbsp;&nbsp;英语资源 ll
total 16312
drwxr-xr-x&nbsp;&nbsp;5 crifan&nbsp;&nbsp;staff&nbsp;&nbsp;&nbsp;160B&nbsp;&nbsp;3 27 14:15 All Aboard Reading
...
-rw-r--r--&nbsp;&nbsp;1 crifan&nbsp;&nbsp;staff&nbsp;&nbsp;&nbsp;8.0M&nbsp;&nbsp;3 28 17:37 downloaded Otto the Cat.MP3
...

【总结】

此处,通过–local(或-l)指定本地的文件名

-》则put时就可以单独指定额外的要存入的文件名了

举例:

mongofiles -d gridfs put "Otto the Cat.MP3" --local "/Users/crifan/dev/dev_root/company/xxx/英语资源/All Aboard Reading/音 频/Otto the Cat.MP3"

效果:

db.fs.files.find().pretty()
{
&nbsp;&nbsp;&nbsp;&nbsp;"_id" : ObjectId("5abb61ffa4bc7116a6fa31d4"),
&nbsp;&nbsp;&nbsp;&nbsp;"chunkSize" : 261120,
&nbsp;&nbsp;&nbsp;&nbsp;"uploadDate" : ISODate("2018-03-28T09:35:59.783Z"),
&nbsp;&nbsp;&nbsp;&nbsp;"length" : 8338105,
&nbsp;&nbsp;&nbsp;&nbsp;"md5" : "b7660d833085e9e1a21813e4d74b0cc3",
&nbsp;&nbsp;&nbsp;&nbsp;"filename" : "Otto the Cat.MP3"
}

则get时就可以单独指定要保存出来的文件名了

举例:

mongofiles -d gridfs get "Otto the Cat.MP3" --local "downloaded Otto the Cat.MP3"

然后当前文件夹中就有了:downloaded Otto the Cat.MP3

转载请注明:在路上 » 【已解决】mongofiles中put保存和get下载获取时指定文件名

发表我的评论
取消评论

表情

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

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
55 queries in 0.077 seconds, using 9.99MB memory