user1370497
user1370497

Reputation: 15

couchDB update attachments

Can I update the attachments of an existing document without uploading them again?

I tried:

<?php
    $json = '
    {
        "_id":"attachment_doc",
        "_rev":'.$rev.',
        "_attachments":
        {
            "free-wallpaper-31.jpg":
            {
                "revpos":3,
                "content_type":"image\/jpeg",
                "data": "'.base64_encode(file_get_contents('free-wallpaper-31.jpg')).'"
            }
        }
    }
';

$doc = $couchdb->doc_upload_attachments('16beb67b990604791b3ffde7dd000576', $json);
var_dump($doc);
?>

But that overwrites the existing attachments and stores the document with a new revision.

Upvotes: 1

Views: 2263

Answers (1)

Marcin Sk&#243;rzewski
Marcin Sk&#243;rzewski

Reputation: 2854

I am not familiar with the API for PHP, but in general in CouchDB REST API you can upload an attachment without uploading the document and other previously uploaded attachments. Take a look at attachment section of API chapter in The Definite Guide. In command line:

curl -vX PUT http://localhost:5984/YOUR_DB/YOUR_DOCUMENT/YOUR_ATTACHMENT.jpg?rev=REVISION_OF_THE_DOCUMENT_BEFORE_UPLOADING_AN_ATTACHMENT --data-binary @FILE_ON_THE_DISK.JPG -H "Content-Type: image/jpg"

It works for me :) And yes, the revision of the document has to change when uploading succeeds. If you have trouble uploading with the bindings in your language, just upload it with standard HTTP API.

Upvotes: 4

Related Questions