Nuxeo API Python client attachBlob, encode error ?

Hi,

I find the correct method to attach a blob with the python automation client (answer how-to-call-blobattach-via-rest-api-in-python)
Perfect for ascii files, but when i try to attach a blob like a pdf or a csv, I have an error UnicodeDecodeError: 'ascii' codec can't decode byte 0xe9 in position 464: ordinal not in range(128) I am on Ubuntu 12.04 / python 2.7.6 / Nuxeo 5.8

Someone have an idea ?

Thanks

Trace :

ERROR:root:'ascii' codec can't decode byte 0xe9 in position 464: ordinal not in range(128)
Traceback (most recent call last):
File "perso_test2.py", line 21, in <module>
s.attachBlob(doc['uid'],'/home/administrateur/test3.csv')
File "/usr/local/lib/python2.7/dist-packages/nuxeo_automation-0.1-py2.7.egg/nuxeoautomation.py", line 93, in attachBlob
    return self._attach_blob(blob, document=ref)
  File "/usr/local/lib/python2.7/dist-packages/nuxeo_automation-0.1-py2.7.egg/nuxeoautomation.py", line 183, in _attach_blob
    resp = self.opener.open(req)
  File "/usr/lib/python2.7/urllib2.py", line 400, in open
    response = self._open(req, data)
  File "/usr/lib/python2.7/urllib2.py", line 418, in _open
    '_open', req)
  File "/usr/lib/python2.7/urllib2.py", line 378, in _call_chain
    result = func(*args)
  File "/usr/lib/python2.7/urllib2.py", line 1207, in http_open
    return self.do_open(httplib.HTTPConnection, req)
  File "/usr/lib/python2.7/urllib2.py", line 1174, in do_open
    h.request(req.get_method(), req.get_selector(), req.data, headers)
  File "/usr/lib/python2.7/httplib.py", line 958, in request
    self._send_request(method, url, body, headers)
  File "/usr/lib/python2.7/httplib.py", line 992, in _send_request
    self.endheaders(body)
  File "/usr/lib/python2.7/httplib.py", line 954, in endheaders
    self._send_output(message_body)
  File "/usr/lib/python2.7/httplib.py", line 812, in _send_output
    msg += message_body
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe9 in position 464: ordinal not in range(128)
0 votes

2 answers

964 views

ANSWER



Hi,

I would suggest you use the latest version of the Python Automation client available in the python-automation-client branch of the https://github.com/nuxeo/nuxeo-automation-clients repository:

https://github.com/nuxeo/nuxeo-automation-clients/tree/python-automation-client/python

This is the result of the extraction of the Python Automation client used by Nuxeo Drive, which we put in a branch for the moment as work is still in progress. You will find:

  • A README
  • The Pyhton Automation client: automation_client.py

If you want to attach a blob to an existing Nuxeo document, the following lines should do:

import automation_client
client = automation_client.BaseAutomationClient('http://server:port/nuxeo', 'usernamme', password='*****')
blob = client.execute_with_blob_streaming('Blob.Attach', '/path/to/the/file/to/upload/my_file.pdf', filename='my_file.pdf', document='/default-domain/workspaces/TestPython/TestPDFUpload')

This should solve your problem, and has the advantage of:

  • Using the latest Automation batch upload API (see http://doc.nuxeo.com/x/OYLZ) instead of a multipart POST
  • Streaming the blob instead of fully loading it in memory!

Hope this helps!

1 votes



Great ! To add in the python client documentation

Thanks a lot

0 votes