API Reference

Helper Functions

Helper methods for Cloud Storage.

cloudstorage.helpers.file_checksum(filename, hash_type='md5', block_size=4096)[source]

Returns checksum for file.

from cloudstorage.helpers import file_checksum

picture_path = '/path/picture.png'
file_checksum(picture_path, hash_type='sha256')
# '03ef90ba683795018e541ddfb0ae3e958a359ee70dd4fccc7e747ee29b5df2f8'

Source: get-md5-hash-of-big-files-in-python

Parameters
  • filename (str or FileLike) – File path or stream.

  • hash_type (str) – Hash algorithm function name.

  • block_size (int) – (optional) Chunk size.

Return type

HASH

Returns

Hash of file.

Raises

RuntimeError – If the hash algorithm is not found in hashlib.

Changed in version 0.4: Returns _hashlib.HASH instead of HASH.hexdigest().

cloudstorage.helpers.file_content_type(filename)[source]

Guess content type for file path or file like object.

Parameters

filename (str or file) – File path or file like object.

Returns

Content type.

Return type

str or None

cloudstorage.helpers.parse_content_disposition(data)[source]

Parse Content-Disposition header.

Example:

>>> parse_content_disposition('inline')
('inline', {})

>>> parse_content_disposition('attachment; filename="foo.html"')
('attachment', {'filename': 'foo.html'})

Source: pyrates/multifruits

Parameters

data (str) – Content-Disposition header value.

Returns

Disposition type and fields.

Return type

tuple

cloudstorage.helpers.read_in_chunks(file_object, block_size=4096)[source]

Return a generator which yields data in chunks.

Source: read-file-in-chunks-ram-usage-read-strings-from-binary-file

Parameters
  • file_object (file object) – File object to read in chunks.

  • block_size (int) – (optional) Chunk size.

Yield

The next chunk in file object.

Yield type

bytes

Return type

Generator[Union[bytes, str], None, None]

cloudstorage.helpers.validate_file_or_path(filename)[source]

Return filename from file path or from file like object.

Source: rackspace/pyrax/object_storage.py

Parameters

filename (str or file) – File path or file like object.

Returns

Filename.

Return type

str or None

Raises

FileNotFoundError – If the file path is invalid.

Utility Functions

Utility methods for Cloud Storage.

cloudstorage.utils.rgetattr(obj, attr, default=<object object>)[source]

Get a nested named attribute from an object.

Example:

b = type('B', (), {'c': True})()
a = type('A', (), {'b': b})()
# True

Source: getattr-and-setattr-on-nested-objects

Parameters
  • obj (object) – Object.

  • attr (str) – Dot notation attribute name.

  • default (object) – (optional) Sentinel value, defaults to object().

Returns

Attribute value.

Return type

object

cloudstorage.utils.rsetattr(obj, attr, val)[source]

Sets the nested named attribute on the given object to the specified value.

Example:

b = type('B', (), {'c': True})()
a = type('A', (), {'b': b})()
rsetattr(a, 'b.c', False)
# False

Source: getattr-and-setattr-on-nested-objects

Parameters
  • obj (object) – Object.

  • attr (str) – Dot notation attribute name.

  • val (object) – Value to set.

Returns

NoneType

Return type

None

Exceptions

Exceptions for Cloud Storage errors.

exception cloudstorage.exceptions.CloudStorageError(message)[source]

Base class for exceptions.

exception cloudstorage.exceptions.NotFoundError(message)[source]

Raised when a container or blob does not exist.

exception cloudstorage.exceptions.IsNotEmptyError(message)[source]

Raised when the container is not empty.

exception cloudstorage.exceptions.CredentialsError(message)[source]

Raised when driver credentials are invalid.

exception cloudstorage.exceptions.SignatureExpiredError[source]

Raised when signature timestamp is older than required maximum age.

Logging

By default, Cloud Storage logs to logging.NullHandler. To attach a log handler:

import logging

logger = logging.getLogger('cloudstorage')
logger.setLevel(logging.DEBUG)

ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)

formatter = logging.Formatter(
    '%(asctime)s - %(name)s.%(funcName)s - %(levelname)s - %(message)s')

ch.setFormatter(formatter)
logger.addHandler(ch)