折腾:
【已解决】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
试了试:
➜ 英语资源 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 connected to: localhost added file: Otto the Cat
然后是可以的指定名字和路径的。
再去加上后缀:
➜ 英语资源 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 connected to: localhost added file: Otto the Cat.MP3
可以查到:
&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/xxx/英语资源/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/xxx/英语资源/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" }
然后再去试试,get输出时,也是可以指定输出文件名的:
英语资源 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 ...
【总结】
此处,通过–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() { "_id" : ObjectId("5abb61ffa4bc7116a6fa31d4"), "chunkSize" : 261120, "uploadDate" : ISODate("2018-03-28T09:35:59.783Z"), "length" : 8338105, "md5" : "b7660d833085e9e1a21813e4d74b0cc3", "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