Couchdb python - Upload Attachment with put_attachment

31 Views Asked by At

I am trying to attach an image to an existing document on couchdb using python. It is attached, but the following error message appears: "Cannot be displayed because it contains an error". The length is also incorrect. Is the coding probably wrong? But how can it be done correctly?

If I attach it directly in Futon:

 "_attachments": {
    "Scan.png": {
      "content_type": "image/png",
      "revpos": 35,
      "digest": "md5-t20y23eHOJOMDAwCcrTjOw==",
      "length": 199281,                             // in python "length": 8
      "stub": true
    }
  }
import couchdb 
couch = couchdb.Server('http://a73657.berlin.de:5984')
db = couch['vl'] # existing

json_file = "test_file"

doc = db.get(json_file) 


db.put_attachment(doc, 'Scan.png', 'Scan.png', content_type="image/png;base64")
2

There are 2 best solutions below

0
smathy On

According to the docs the content argument (the second arg) is "either a file-like object or a string", you've provided the second option, so the content of your attachment is the string "Scan.png" (hence the 8 char length).

You're probably trying to use something more like this:

db.put_attachment(doc, open('Scan.png', 'rb'), 'Scan.png', content_type="image/png;base64")
0
Ute On

I have found a solution:

content=pathlib.Path('C:/Users/ute/python/TL1/Scan.png').read_bytes() 

db.put_attachment(doc, content, "Scan.png" , content_type="image/png")