How to download Couchdb file attachments?

I am converting my sqlite database to Couchdb. I can convert db and upload to Couchdb server. Everything except images. I want to upload 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 one file for testing, and it works. How to make volume?

This is an iOS application developed using Appcelerator Titanium.

+4
source share
2 answers

You can configure one file and do something like the following:

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

with data:

 { "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 may encounter problems. Perhaps it would be better to just loop through and do them one at a time; or at least in batches with a reasonable size.

+1
source

Simple work around. Export all data first to first. Then create a column that will have the exact URLs needed to execute the curl statements for each document (in excel there will be rows for each document). A simple curl statement to perform image loading:

 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 creating the individual curl URLs, which will be pretty easy in excel, copy the entire column of embedded URLs into a text file and put the text file in which the images to be uploaded will be loaded. Then create a bash script that will read all the lines and continue to post 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 url lines are present in the test.txt file.

This method worked perfectly for me, the image size was about 60-65 kilobytes, but with 380 documents. Not sure what will happen to large files.

0
source

Source: https://habr.com/ru/post/1386772/


All Articles