Attachments

Attachments are independent binary data attached to a document. They are file-like and require a name and the content type. As attachments do not have size restrictions, they are handled a bit differently than documents in the Document class. The content of the attachment isn’t cached in the instance at any point, thus data access require a network request.

Getting an Attachment instance

Given a document instance, we can get an Attachment instance using the attachment() member function. Unlike with Document instances, no data is retrieved from the sever yet.

butterfly = await database["butterfly"]
image_of_a_butterfly = butterfly.attachment("image.png")

Retrieving the Attachment content

To actually retrieve the data stored on the server, you have to use the fetch() method. Once the fetch method is called, the content_type member will be set to appropriate value passed from the server.

data = await image_of_a_butterfly.fetch()

Saving the content of an attachment

Reference

class aiocouch.attachment.Attachment(document, id)

A local representation for the referenced CouchDB document attachment

An instance of this class represents a local copy of an attachment of CouchDB documents.

Variables
  • id – the id of the attachment

  • content_type – the content type of the attachment, only available after fetch() has been called.

Parameters
  • document (Document) – The correlated document

  • id (str) – the id of the attachment

await delete()

Deletes the attachment from the server

Return type

None

await exists()

Checks if the attachment exists on the server

Return type

bool

Returns

returns True if the attachment exists

await fetch()

Returns the content of the attachment

Return type

bytes

Returns

the attachment content

await save(data, content_type)

Saves the given attachment content on the server

Parameters
  • data (bytes) – the content of the attachment

  • content_type (str) – the content type of the given data. (See Content type)

Return type

None