【发布时间】:2016-01-01 11:06:37
【问题描述】:
我正在尝试检索使用 GridFS 成功存储的二进制文件。 我在 Mac OS X 上使用 MongoDB v3.0.6、Ruby 2.0.0 和 MongoDB Ruby Driver v 2.0.1。
有没有可行的例子?
【问题讨论】:
我正在尝试检索使用 GridFS 成功存储的二进制文件。 我在 Mac OS X 上使用 MongoDB v3.0.6、Ruby 2.0.0 和 MongoDB Ruby Driver v 2.0.1。
有没有可行的例子?
【问题讨论】:
查看文档,这似乎应该可行:
client = Mongo::Client.new([ '127.0.0.1:27017' ], :database => 'music')
client.database.fs.find_one(:filename => 'new-file.txt') #=> Returns a Mongo::Grid::File
以下是将其流式传输到文件的方法:
client.database.fs.open_download_stream(file_id) do |stream|
IO.write('some-file', stream.read)
end
【讨论】:
以下代码有效:
require 'rubygems'
require 'mongo'
include Mongo
$client = Mongo::Client.new([ '127.0.0.1:27017' ], :database => 'garden')
Mongo::Logger.logger.level = ::Logger::ERROR
$files = $client[:files]
puts 'connected!'
# Upload file
fs = $client.database.fs
$file = File.open("delete.rb")
$file_id = fs.upload_from_stream("delete.rb", $file)
$file.close
$file_to_write = File.open('perfectCopy', 'w')
fs.download_to_stream($file_id, $file_to_write)
米哈利斯。
【讨论】: