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
- 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.
-
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
-
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
-
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
- 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
Exceptions¶
Exceptions for Cloud Storage errors.
-
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.
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)