parameters to put_attachment

506 views
Skip to first unread message

harsha

unread,
Jul 2, 2010, 2:59:26 AM7/2/10
to CouchDB-Python
Hello,
I am trying to add an attachment to an existing CouchDB document
through couchdb-python. Documentation says put_attachment() needs the
doc, content, filename and content type (in that order) as the
function input.

I was wondering if one can give the file system (say /home/foo/
bar.jpg) path for the content parameter or it is necessary to provide
it in an ascii base64 format?

I am using v0.6-1 with Ubuntu Lucid Lynx and I can upload binary
attachments to CouchDB documents by converting it to using
binascii.b2a_base64(). Providing a filesystem path does not seem to
work.

Thanks in advance,
harsha

Matt Goodall

unread,
Jul 2, 2010, 4:14:45 AM7/2/10
to couchdb...@googlegroups.com

Hi Harsha,

The 'content' parameter to put_attachment is either a file-like object
to read data from or the data itself as a binary string. If you pass a
file name it will be treated as a binary string, i.e. the attachment's
content.

However, there's no need to base64 anything if you're sending
something on the file system:

f = open(filename)
try:
db.put_attachment(doc, f, filename)
finally:
f.close()

Note that if you're adding large attachments then you should probably
look at upgrading to couchdb-python 0.7 as it streams the attachment's
data instead of reading it all into memory.

- Matt

Reply all
Reply to author
Forward
0 new messages