【问题标题】:Standalone Attachments in CouchDB + RubyCouchDB + Ruby 中的独立附件
【发布时间】:2010-04-20 14:27:11
【问题描述】:
有没有人成功地使用独立附件 API(如果可能是 gzip 格式)从 ruby 将独立附件发送到 couchDB?
我知道有一些 CURL 示例,但到目前为止,我对 Typhoeus 的尝试还没有成功。它通常会在前几个文档之后停止并等待 > 1 分钟。
CouchRest 似乎不支持它,我看过的任何其他库也不支持
编辑:澄清
我不是在寻找常规的 Base64 编码附件。 CouchRest 做得很好。
【问题讨论】:
标签:
ruby
couchdb
attachment
【解决方案1】:
得到它与台风的工作
Typhoeus::Request.put("http://127.0.0.1:5984/db/document/my_attachment_name?rev=#{rev}", "content-type" => "text/html", "Content-Encoding" => "gzip", "Accept-Encoding" => "gzip", :body => my_html_body)
这会将“my_html_body”字符串作为 gzip 独立附件存储到 couchDB 中
【解决方案2】:
对于二进制独立附件,我只是使用 IO.read("/path/to/my/file") 将字符串作为 :body 提供给 put 方法。看起来它正在工作,但我不知道这是否是正确的方法。
看起来像这样:
res = Typhoeus::Request.get("http://localhost:5984/_uuids")
uuid = JSON.parse(res.body)["uuids"].first
doc = {}
doc["name"] = name
...
res = Typhoeus::Request.put("http://localhost:5984/products/#{uuid}", :body => JSON.generate(doc))
res = Typhoeus::Request.put("http://localhost:5984/products/#{uuid}/image.jpg?rev=#{rev}", :headers => {"Content-Type" => "image/jpeg" }, :body => IO.read("output/images/#{image}"))