Blobs

    +

    Description — Working with Couchbase Lite’s data model — handling data store attachments blobs’s
    Related Content — Databases | Documents | Indexes

    What Are Blobs?

    We’ve renamed "attachments" to "blobs". The new behavior should be clearer too, as a Blob is now a normal object that can appear in a document as a property value.

    Using Blobs

    So to uses a blob, you just instantiate it and set it as the value of a property. Later you can get the property value, which will be a Blob object.

    Example 1. Add a blob to a document

    This example shows code that adds a blob to the document under the avatar property.

    InputStream is = getAsset("avatar.jpg");
    if (is == null) { return; }
    try {
        Blob blob = new Blob("image/jpeg", is);
        newTask.setBlob("avatar", blob);
        database.save(newTask);
    
        Blob taskBlob = newTask.getBlob("avatar");
        byte[] bytes = taskBlob.getContent();
    } catch (CouchbaseLiteException e) {
        Log.e(TAG, e.toString());
    } finally {
        try { is.close(); }
        catch (IOException ignore) { }
    }

    The Blob API lets you access the contents as an in-memory byte array ( public byte[] getContent()) or as an InputStream (public InputStream getContentStream()). It also supports an optional type property that by convention stores the MIME type of the contents.

    In Example 1, "image/jpeg" is the MIME type and "avatar" is the key which references that Blob. That key can be used to retrieve the Blob object at a later time.

    Synchronization Behaviour

    When a document is synchronized, the Couchbase Lite replicator will add an _attachments dictionary to the document’s properties if it contains a blob.

    A random access name will be generated for each Blob which is different to the "avatar" key that was used in the example above.

    On the image below, the document now contains the _attachments dictionary when viewed in the Couchbase Server Admin Console.

    attach replicated

    This Blob can be retrieved on the Sync Gateway REST API at http://localhost:4984/justdoit/user.david/blob_1. Notice that the blob identifier in the URL path is "blob_1" (not "avatar").

    Properties

    A blob also has properties such as "digest" (a SHA-1 digest of the data), "length" (the length in bytes), and optionally "content_type" (the MIME type). The data is not stored in the document, but in a separate content-addressable store, indexed by the digest.