CouchDB 通过命令行即时添加附件

发布于 2024-12-11 06:11:26 字数 850 浏览 0 评论 0原文

问题

我希望能够在创建文档时通过命令行附加一个/多个附件(见下文)。我只能在 Futon (Couchbase) 中使用它,但前提是已经创建了文档。

我已尝试以下操作:

curl -X PUT 'http://username:password@localhost:5984/client_info'

curl -X POST 'http://username:password@localhost:5984/client_info' -H 'Content-Type: application/json' -d '{"client_type": "Private", "client_name": "John Doe","client_email": "[email protected]","client_city": "Toronto","created_at": "2011-09-06 12:45:03","expires_at": "2012-01-01 00:00:00", "_attachments": {
   "test01.jpg": {
       "content_type": "image/jpeg",
       "length": 30189          
    }
  }
}'

这只会导致以下错误:

{"error":"unknown_error","reason":"function_clause"}

谢谢

PROBLEM

I want to be able to attach one/multiple attachment(s) as the document is created, through the command-line (see below). I can only get this to work in Futon (Couchbase), but only after a document has already been created.

I have tried the following:

curl -X PUT 'http://username:password@localhost:5984/client_info'

curl -X POST 'http://username:password@localhost:5984/client_info' -H 'Content-Type: application/json' -d '{"client_type": "Private", "client_name": "John Doe","client_email": "[email protected]","client_city": "Toronto","created_at": "2011-09-06 12:45:03","expires_at": "2012-01-01 00:00:00", "_attachments": {
   "test01.jpg": {
       "content_type": "image/jpeg",
       "length": 30189          
    }
  }
}'

This only results in the following error:

{"error":"unknown_error","reason":"function_clause"}

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

我喜欢麦丽素 2024-12-18 06:11:26

您必须在单独的步骤中上传附件,并在请求正文中包含实际的附件文件。因此,首先创建常规文档,然后在上传文件时发出另一个请求。以下是如何使用curl上传附件的示例(http://guide.couchdb.org/draft/api.html#attachments):curl -v -X PUT http://127.0.0.1:5984/albums/6e1295ed6c29495e54cc05947f18c8af/artwork.jpg?rev=2-2739352689 --data-binary @artwork.jpg -H "Content-Type: image/jpg"

这里是附件的官方 API:http://wiki.apache.org/couchdb/HTTP_Document_API#Standalone_Attachments

You must upload your attachment in a separate step, containing the actual attachment file in the request body. So first create your regular document, then issue another request where you upload the file. Here is an example on how to upload an attachment using curl (http://guide.couchdb.org/draft/api.html#attachments): curl -v -X PUT http://127.0.0.1:5984/albums/6e1295ed6c29495e54cc05947f18c8af/artwork.jpg?rev=2-2739352689 --data-binary @artwork.jpg -H "Content-Type: image/jpg"

And here is the official API for attachments: http://wiki.apache.org/couchdb/HTTP_Document_API#Standalone_Attachments

拥醉 2024-12-18 06:11:26

这对我有用,而且看起来更简单一些。如果您不添加修订版本,第一个必须是在创建文档时。我的示例使用数据库“test1”。

$ curl -H "Content-Type: image/jpeg" -X PUT --data-binary @test01.jpg 'http://username:password@localhost:5984/test1/client_info/test01.jpg'

{"ok":true,"id":"client_info","rev":"1-8584b6af9d0c3347ba08202697f09952"}

$ curl -H "Content-Type: image/jpeg" -X PUT --data-binary @test02.jpg 'http://username:password@localhost:5984/test1/client_info/test02.jpg?rev=1-8584b6af9d0c3347ba08202697f09952'

{"ok":true,"id":"client_info","rev":"2-623b94aba30944d6744f5c11cf03fc10"}

This works for me, and seems a little simpler. The first one has to be when creating the doc, if you don't add a rev. My examples uses database "test1".

$ curl -H "Content-Type: image/jpeg" -X PUT --data-binary @test01.jpg 'http://username:password@localhost:5984/test1/client_info/test01.jpg'

{"ok":true,"id":"client_info","rev":"1-8584b6af9d0c3347ba08202697f09952"}

$ curl -H "Content-Type: image/jpeg" -X PUT --data-binary @test02.jpg 'http://username:password@localhost:5984/test1/client_info/test02.jpg?rev=1-8584b6af9d0c3347ba08202697f09952'

{"ok":true,"id":"client_info","rev":"2-623b94aba30944d6744f5c11cf03fc10"}
梦幻的味道 2024-12-18 06:11:26

这是一种在创建文档的同一请求中上传附件的方法。

curl -X POST 'http://user:pass@localhost:5984/client_stuff' -H 'Content-Type: application/json' -d '{"stuff": "stuff", "_attachments": {
   "empty.gif": {
       "content_type": "image/gif",
       "data": "'$(openssl base64 < file.gif)'"
    }
  }
}'

根据您的使用情况,Base64 编码可能不会那么糟糕。

更多信息:http://wiki.apache.org/couchdb/HTTP_Document_API#Inline_Attachments

Here is a way to upload an attachment in the same request as the creation of the document.

curl -X POST 'http://user:pass@localhost:5984/client_stuff' -H 'Content-Type: application/json' -d '{"stuff": "stuff", "_attachments": {
   "empty.gif": {
       "content_type": "image/gif",
       "data": "'$(openssl base64 < file.gif)'"
    }
  }
}'

Depending on your use case, the Base64-encoding may not be so bad.

More info: http://wiki.apache.org/couchdb/HTTP_Document_API#Inline_Attachments

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文