1.4.2. /db/doc/attachment

HEAD /{db}/{docid}/{attname}

返回包含有关指定附件的最少信息的HTTP头。该方法支持与 :get:`/{{db}}/{{docid}}/{{attname}}` 方法,但只有头信息(包括附件大小、编码和作为 ETag ),返回。

参数:
  • db -- 数据库名称
  • docid -- 文档ID
  • attname -- 附件名称
请求标头:
  • If-Match -- 文档的修订版本。替代 rev 查询参数
  • If-None-Match -- 附件的base64编码的MD5二进制摘要。 可选的
查询参数:
  • rev (string) -- 文件的修订。 可选的
响应头:
状态代码:

请求

HEAD /recipes/SpaghettiWithMeatballs/recipe.txt HTTP/1.1
Host: localhost:5984

响应

HTTP/1.1 200 OK
Accept-Ranges: none
Cache-Control: must-revalidate
Content-Encoding: gzip
Content-Length: 100
Content-Type: text/plain
Date: Thu, 15 Aug 2013 12:42:42 GMT
ETag: "vVa/YgiE1+Gh0WfoFJAcSg=="
Server: CouchDB (Erlang/OTP)
GET /{db}/{docid}/{attname}

返回与文档关联的文件附件。将返回关联附件的原始数据(就像访问静态文件一样)。返回的 Content-Type 将与文档附件提交到数据库时设置的内容类型相同。

参数:
  • db -- 数据库名称
  • docid -- 文档ID
  • attname -- 附件名称
请求标头:
  • If-Match -- 文档的修订版本。替代 rev 查询参数
  • If-None-Match -- 附件的base64编码的MD5二进制摘要。 可选的
查询参数:
  • rev (string) -- 文件的修订。 可选的
响应头:
响应:

存储的内容

状态代码:
PUT /{db}/{docid}/{attname}

将提供的内容作为附件上载到指定文档。提供的附件名称必须是URL编码的字符串。必须提供内容类型标头,对于现有文档,还必须提供 rev 查询参数或 If-Match HTTP标头。如果省略修订,则将使用提供的附件创建一个新的、否则为空的文档,否则将发生冲突。

如果在使用现有附件名上载附件时发生这种情况,CouchDB将更新数据库的相应存储内容。由于必须提供修订信息才能将附件添加到文档中,因此这将作为更新现有附件的验证。

注解

上载附件将更新相应的文档修订。跟踪父文档的修订,而不是单个附件。

参数:
  • db -- 数据库名称
  • docid -- 文档ID
  • attname -- 附件名称
请求标头:
  • Content-Type -- Attachment MIME type. Default: application/octet-stream Optional
  • If-Match -- 文档修订。替代 rev 查询参数
查询参数:
  • rev (string) -- 文件修订。 可选的
响应JSON对象:
  • id (string) -- 文档ID
  • ok (boolean) -- 运行状态
  • rev (string) -- 修订MVCC令牌
状态代码:

请求

PUT /recipes/SpaghettiWithMeatballs/recipe.txt HTTP/1.1
Accept: application/json
Content-Length: 86
Content-Type: text/plain
Host: localhost:5984
If-Match: 1-917fa2381192822767f010b95b45325b

1. Cook spaghetti
2. Cook meatballs
3. Mix them
4. Add tomato sauce
5. ...
6. PROFIT!

响应

HTTP/1.1 201 Created
Cache-Control: must-revalidate
Content-Length: 85
Content-Type: application/json
Date: Thu, 15 Aug 2013 12:38:04 GMT
ETag: "2-ce91aed0129be8f9b0f650a2edcfd0a4"
Location: http://localhost:5984/recipes/SpaghettiWithMeatballs/recipe.txt
Server: CouchDB (Erlang/OTP)

{
    "id": "SpaghettiWithMeatballs",
    "ok": true,
    "rev": "2-ce91aed0129be8f9b0f650a2edcfd0a4"
}
DELETE /{db}/{docid}/{attname}

删除文件名为的附件 {{attname}} 指定的 doc . 你必须提供 rev 查询参数或 If-Match 使用当前修订删除附件。

注解

删除附件将更新相应的文档修订。跟踪父文档的修订,而不是单个附件。

参数:
  • db -- 数据库名称
  • docid -- 文档ID
请求标头:
  • Accept --
    • application/json
    • text/plain
  • If-Match -- 文档修订。替代 rev 查询参数
查询参数:
  • rev (string) -- 文件修订。 要求的
  • batch (string) -- 将更改存储在 batch mode 可能值: ok . 可选的
响应头:
  • Content-Type --
    • application/json
    • text/plain; charset=utf-8
  • ETag -- 双引号文件的新修订
响应JSON对象:
  • id (string) -- 文档ID
  • ok (boolean) -- 运行状态
  • rev (string) -- 修订MVCC令牌
状态代码:

请求

DELETE /recipes/SpaghettiWithMeatballs?rev=6-440b2dd39c20413045748b42c6aba6e2 HTTP/1.1
Accept: application/json
Host: localhost:5984

或者,代替 rev 可以使用的查询参数 If-Match 标题:

DELETE /recipes/SpaghettiWithMeatballs HTTP/1.1
Accept: application/json
If-Match: 6-440b2dd39c20413045748b42c6aba6e2
Host: localhost:5984

响应

HTTP/1.1 200 OK
Cache-Control: must-revalidate
Content-Length: 85
Content-Type: application/json
Date: Wed, 14 Aug 2013 12:23:13 GMT
ETag: "7-05185cf5fcdf4b6da360af939431d466"
Server: CouchDB (Erlang/OTP)

{
    "id": "SpaghettiWithMeatballs",
    "ok": true,
    "rev": "7-05185cf5fcdf4b6da360af939431d466"
}

1.4.2.1. HTTP范围请求

HTTP允许您指定请求的字节范围。这允许实现可恢复的下载和可跳过的音频和视频流。这可用于CouchDB内的所有附件。

这只是一个真正的快速运行,看看如何在引擎盖下。通常,CouchDB会提供更大的二进制文件,比如mp3和视频,但是为了让事情变得更明显,我在这里使用了一个文本文件(注意,我使用了 application/octet-stream :header`Content-Type` instead of text/plain

shell> cat file.txt
My hovercraft is full of eels!

现在让我们将这个文本文件作为附件存储在CouchDB中。首先,我们创建一个数据库:

shell> curl -X PUT http://127.0.0.1:5984/test
{"ok":true}

然后我们一次性创建新文档和文件附件:

shell> curl -X PUT http://127.0.0.1:5984/test/doc/file.txt \
            -H "Content-Type: application/octet-stream" -d@file.txt
{"ok":true,"id":"doc","rev":"1-287a28fa680ae0c7fb4729bf0c6e0cf2"}

现在我们可以轻松地请求整个文件:

shell> curl -X GET http://127.0.0.1:5984/test/doc/file.txt
My hovercraft is full of eels!

但假设我们只需要前13个字节:

shell> curl -X GET http://127.0.0.1:5984/test/doc/file.txt \
            -H "Range: bytes=0-12"
My hovercraft

HTTP支持多种指定单字节甚至多字节范围的方法。把所有的信息都读进去 RFC 2616#section-14.27 .

注解

使用couchdb1.0.2或更早版本创建的数据库将支持 3.1 ,但他们使用的是一种不太理想的算法。如果您计划大量使用此功能,请确保使用CouchDB压缩数据库 3.1 利用更好的算法来查找字节范围。