Exemplo n.º 1
0
storage_client.container = 'pithos'

obj = raw_input('Pick object to stream: ')
destination = raw_input('Stream it where? ')

obj_size = int(storage_client.get_object_info(obj)['content-length'])
BLOCK_SIZE = int(storage_client.get_container_info()['x-container-block-size'])
CHUNK_SIZE = 4 * BLOCK_SIZE


def stream(i, output):
    """Stream the contents of buf[i] to output"""
    output.write(bufs[i])


from kamaki.clients import SilentEvent

with open(destination, 'w+') as output:
    event = None
    bufs = ['', '']
    for i in range(1 + (obj_size / CHUNK_SIZE)):
        buf_index = i % 2
        start, end = CHUNK_SIZE * i, min(CHUNK_SIZE * (i + 1), obj_size)
        bufs[buf_index] = storage_client.download_to_string(obj,
                                                            range_str='%s-%s' %
                                                            (start, end))
        if event and not event.is_alive():
            event.join()
        event = SilentEvent(stream, buf_index, output)
        event.start()