Sync Gateway Update Blob

I am making the following call to update an image via the Sync Gateway (withOUT using the mobile components) and while the return status is OK, the BLOB never gets updated.

PUT http://api.redbookfuneraldirectory.com:4984/ebookh/Directory~1d41e77f-f7e5-e511-80e5-002590701742/blob_%2FAdImage?rev=7-5cfd9eb0ad849b5ea0831169264cb0df HTTP/1.1
Authorization: Basic cmVkYm9vay1zeW5jOjEwN2RkMTkwYzI3ZGUxNjgzNzE2MDI1NWU3ZmU5YjljOTdkNGVjMjQ3YzkwYjg1NmQ1Yzg4NjVhZDRlNWIyMjQ2YWIzMGY=
Content-Type: image/png
Host: api.redbookfuneraldirectory.com:4984
Content-Length: 39906
Expect: 100-continue
(contents eliminated but there are 39906 bytes)

TL;DR: The last path component in the URL is probably wrong. To check, do a GET request of the document, and look at the key in the _attachments sub-dictionary. That key string is the path component you should use.

Details: Sync Gateway doesn’t yet support the newer blob schema introduced in Couchbase Lite 2. Instead, it expects attachments to be in an _attachments sub-dictionary. When CBL pushes a doc to SG, it creates this sub-dictionary and creates shadow copies of the blobs in it. (The reverse happens when it pulls from SG.)

So in SG, your blob is going to appear as something like {"_attachments":{"blob1":{"digest":........}}}. The name of the attachment there is blob1, and to PUT to it you should add blob1 as the final path component after the document ID.

We realize this is kind of annoying. There just hasn’t been time to rewrite SG’s attachment handling code to make it work the same way as CBL (especially since SG still supports CBL 1.4, so it’d have to support both schemas.)

Jens, thank you for the explanation. I do think, however, that I am using the correct attachment name. As the GET JSON result shows, my attachment is called blob_/AdImage, which I am using as the final part of my path.

Interestingly, the SG gives me an OK response, but nothing is updated.

image

You’re right, that PUT request should have worked. And it’s especially weird that it would get a 200 response but nothing happens (I assume you checked by doing a GET of the document or the attachment afterwards?) Any idea, @adamf?

I have the same question @jens just asked - how are you attempting to verify whether it was successful afterward? (GET document? GET attachment?) . I suspect the problem might be on that side, particularly around whether the forward slash in the path is being escaped properly on the GET.

I run a query in Couchbase server using the Query Editor and that document returns null for AdImage.

In that case, this is the scenario @jens described above. Attachments created via the REST API in Sync Gateway don’t use the newer blob schema introduced in CBL 2.0, and don’t store the blob metadata within the document directly - it’s stored in the _attachments property.

So is there a way to accomplish what I’m trying to do? I need to add / update an attachment to the server so that it then propagates to all of the clients.

Scenario: I have a central Windows app that talks directly with the server. That app needs to attach an image to a document. That image, in turn, needs to be replicated to all clients.

You can certainly do that. But you’ll need to use the SG REST API to read attachments (data or metadata). I.e. if you send SG a GET request to the document, or the attachment, you should see it’s been updated. And Couchbase Lite will get the updated attachment too.

So issuing this request: GET /ebookh/Directory~1d41e77f-f7e5-e511-80e5-002590701742 give the following response. Does this then tell me that the attachment is there and Couchbase Lite will get it from SG as well?

Is there any to use the server to see / verify the attachment?

image

The attachment with those characteristics is there. I don’t know whether that SHA-1 digest matches the new or old version.

Does this then tell me that the attachment is there and Couchbase Lite will get it from SG as well?

Yes, that’s what I said above.

Is there any to use the server to see / verify the attachment?

No. Couchbase Server itself doesn’t know anything about attachments/blobs. Those are a mobile-only feature.