Blob container API

The connection object provides a convenience API for easy access to blob tables.

Get blob container handle

Create a connection:

>>> from crate.client import connect
>>> client = connect([crate_host])

Get a blob container:

>>> container = client.get_blob_container('myfiles')

Store blobs

The container allows to store a blob without explicitly providing the hash for the blob. This feature is possible if the blob is provided as a seekable stream like object.

Store a StringIO stream:

>>> from io import BytesIO
>>> f = BytesIO(b'StringIO data')
>>> stringio_bob = container.put(f)
>>> stringio_bob

Store from a file:

>>> from tempfile import TemporaryFile
>>> f = TemporaryFile()
>>> _ = f.write(b'File data')
>>> _ =
>>> file_blob = container.put(f)
>>> file_blob
>>> f.close()

If the blob data is not provided as a seekable stream the hash must be provided explicitly:

>>> import hashlib
>>> string_data = b'String data'
>>> string_blob = hashlib.sha1(string_data).hexdigest()
>>> container.put(string_data, string_blob)

Check for existence

>>> container.exists(string_blob)
>>> container.exists('unknown')

Retrieve blobs

Blobs can be retrieved using its hash:

>>> blob_stream = container.get(string_blob)
>>> blob_stream
<generator ...>
>>> data = next(blob_stream)
>>> data == string_data

Delete blobs

Blobs can be deleted using its hash:

>>> container.delete(string_blob)
>>> container.exists(string_blob)

Trying to delete a not existing blob:

>>> container.delete(string_blob)

Close connection

Close the connection to clear the connection pool:

>>> client.close()


How helpful was this page?