1

I'm working on python client examples with Cloudant NoSQLDB. For relatively simple document creation, it's OK. However, trying to upload a base64 encoded file as an attachment to doc, I run into a problem that I cannot solve by myself.
Please help me.

Here is my code.

from cloudant.client import Cloudant
from cloudant.error import CloudantException
from cloudant.result import Result,ResultByKey
import base64
.
.
.

client.connect()
databaseName = "mydata1"
myDatabase = client[databaseName]

targetfile = "chibitest.png"
with open(targetfile,"rb") as fp:
    byte_content = fp.read()
dataContentb= base64.b64encode(byte_content)
dataContent = dataContentb.decode()

jsonDoc = {
    "nameField": "sample1",
    "_attachements":{
      targetfile: 
      {"content-type":"image/png",
       "data":dataContent}
     }
}
newDocument = myDatabase.create_document(jsonDoc)

The result Error is as follows:

raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 400 Client Error: Bad Request doc_validation Bad
special document member: _attachements for url:https://.....cloudant.com/mydata1 

(without "_attachements" section , it works appropriately.)

Additional information.

In the above code, the line;

dataContent = dataContentb.decode()

is the one I needed to solve json error. This is thanks to an article, Serialize in JSON a base64 encoded data

Although, I could not specify "ENCODING" as decode() parameter as is shown in this article (because causing "not defined error"), I guess the resultant dataContent would be OK as far as I can see by inserting print statement..

Chibi
  • 39
  • 6

1 Answers1

0

You are on the right lines, but there is a put_attachment method dedicated to uploading attachments to an existing document.

Here's some working code

from cloudant.client import Cloudant
import base64

# connect
client = Cloudant('USER', 'PASS', account='ACCOUNT', connect=True)
session = client.session()

# create database
my_database = client.create_database('so46442945')

# load attachment
targetfile = "dog.png"
with open(targetfile,"rb") as fp:
    byte_content = fp.read()
dataContentb= base64.b64encode(byte_content)
dataContent = dataContentb.decode()

# Create a document using the Database API
jsonDoc = {
  '_id': 'mydoc', 
  'name': 'Julia',
  'age': 30,
  'pets': ['cat', 'dog', 'frog']
}
my_document = my_database.create_document(jsonDoc)

# Add attachment
my_document.put_attachment('dog.png', 'image/png', dataContent)

# disconnect
client.disconnect()

This creates a database, adds a document and then uploads an image attachment. After the script has finished, the document looks like this:

{
  "_id": "mydoc",
  "_rev": "2-0649fa69ed362501f97a328e4e81ee21",
  "age": 30,
  "name": "Julia",
  "pets": [
    "cat",
    "dog",
    "frog"
  ],
  "_attachments": {
    "dog.png": {
      "content_type": "image/png",
      "revpos": 2,
      "digest": "md5-l5CNi8JJBiQjUd+cS1ZFng==",
      "length": 436572,
      "stub": true
    }
  }
}
Glynn Bird
  • 5,507
  • 2
  • 12
  • 21