user1036437
user1036437

Reputation: 51

How do I bulk upload Couchdb attachments?

I am converting my sqlite database to Couchdb. I can convert the db and upload to the Couchdb server. Everything but the images. I want to upload the images as standalone attachments and I would like to do this in bulk using javascript, REST, and xhr.

POST http://127.0.0.1:5984/database/_bulk_docs

Data : {"_id": "701", "_attachments": {"555_image.png": { "content_type": "image/jpg","data":[object TiFilesystemFile]      }}}

I have CURLed a single file to test and that works. How do I do bulk?

This is and iOS app developed with Appcelerator Titanium.

Upvotes: 5

Views: 1510

Answers (2)

Sunil Biswakarma
Sunil Biswakarma

Reputation: 129

A simple work around..Export all data to excel first. Then create a column which will have the exact urls needed for executing the curl statements for every docs(in the excel there will be rows for every documents). Simple curl statement to execute image upload:

curl -v -X PUT "http://username:password@serverip:portno/dbname/doc_id/image.JPG?rev=revisionid" --data-binary @image.JPG -H "Content-Type:image/jpg"

After building up the individual urls required for curl, which will be pretty easy in excel, copy the entire column of built up urls in a text file and place the text file where the images to be uploaded are present. Then create a bash script to which will read all the lines and keep on posting the images to the couchdb server: bash script

#!/bin/bash
while read line
do
        curl -v -X PUT $line
done < test.txt

In my case all the url lines are present in the test.txt file.

This method has worked for me flawlessly the size of the images were around 60-65kb though with 380 docs. Not sure what will happen with large files.

Upvotes: 0

Colin Ross
Colin Ross

Reputation: 589

You should be able to adjust your single file, and do something like the following:

POST http://127.0.0.1:5984/database/_bulk_docs

with the data being:

{
  "docs": [
    {"_id": "701", "_attachments": {"555_image.png": { "content_type": "image/jpg","data":[object TiFilesystemFile] }},
    {"_id": "702", "_attachments": {"556_image.png": { "content_type": "image/jpg","data":[object TiFilesystemFile] }},
    {"_id": "703", "_attachments": {"557_image.png": { "content_type": "image/jpg","data":[object TiFilesystemFile] }},
  ]
}

However, depending on the number and size of attachments you might run into problems. It might be better to simply loop and do them one at a time; or at least in reasonably sized batches.

Upvotes: 2

Related Questions