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

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

MongoDB crifan 232浏览 0评论

折腾:

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

期间,之前是:

<code>➜  英语资源 mongofiles -d gridfs put "/Users/crifan/dev/dev_root/company/naturling/数据/FromMaggie/英语资源/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/naturling/数据/FromMaggie/英语资源/All Aboard Reading/音频/Otto the Cat.MP3
➜  英语资源 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>

缺点是:

没有指定文件名,导致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

试了试:

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

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

再去加上后缀:

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

可以查到:

<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("5abb61ffa4bc7116a6fa31d4"),
    "chunkSize" : 261120,
    "uploadDate" : ISODate("2018-03-28T09:35:59.783Z"),
    "length" : 8338105,
    "md5" : "b7660d833085e9e1a21813e4d74b0cc3",
    "filename" : "Otto the Cat.MP3"
}
</code>

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

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

【总结】

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

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

举例:

<code>mongofiles -d gridfs put "Otto the Cat.MP3" --local "/Users/crifan/dev/dev_root/company/naturling/数据/FromMaggie/英语资源/All Aboard Reading/音 频/Otto the Cat.MP3"
</code>

效果:

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

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

举例:

<code>mongofiles -d gridfs get "Otto the Cat.MP3" --local "downloaded Otto the Cat.MP3"
</code>

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

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

发表我的评论
取消评论

表情

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

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
15 queries in 0.252 seconds, using 10.27MB memory