import hashlib


def md5_checksum_stream(s, buffer_size=8192):
    """ calculate md5 for a stream in case of a large file that cannot be hold in memory

    :param s: stream
    :return:
    """
    m = hashlib.md5()
    while True:
        data = s.read(buffer_size)
        if not data:
            break
        m.update(data)
    return m.hexdigest()