Search code examples
javacouchdbblobstoring-data

How to store Blob in CouchDB using Java?


I have a blob and want to store it to the Apache CouchDb in java. I get the blob like this

Blob blob1 = blobStore.getBlob(containerName, fileName);

How do I store it in CouchDB?


Solution

  • I don't have a set up to test this out, but looking at the documentation, you want to do something like this:

    ...
    Blob blob = blobStore.getBlob(containerName, fileName);
    InputStream is = blob.getPayload.openStream();
    String contentType = "image/jpeg"; //or whatever content the blob is.
    AttachmentInputStream ais = new AttachmentInputStream("attachment_id", is, contentType);
    db.createAttachment("new_document_id", ais);
    ...
    

    Obviously you would need exception handling and error checking.