def get_file_to_stream()

in azure/multiapi/storage/v2015_04_05/file/fileservice.py [0:0]


    def get_file_to_stream(
        self, share_name, directory_name, file_name, stream,
        start_range=None, end_range=None, range_get_content_md5=None,
        progress_callback=None, max_connections=1, max_retries=5,
        retry_wait=1.0, timeout=None):
        '''
        Downloads a file to a stream, with automatic chunking and progress
        notifications. Returns an instance of :class:`File` with properties
        and metadata.

        :param str share_name:
            Name of existing share.
        :param str directory_name:
            The path to the directory.
        :param str file_name:
            Name of existing file.
        :param io.IOBase stream:
            Opened file/stream to write to.
        :param int start_range:
            Start of byte range to use for downloading a section of the file.
            If no end_range is given, all bytes after the start_range will be downloaded.
            The start_range and end_range params are inclusive.
            Ex: start_range=0, end_range=511 will download first 512 bytes of file.
        :param int end_range:
            End of byte range to use for downloading a section of the file.
            If end_range is given, start_range must be provided.
            The start_range and end_range params are inclusive.
            Ex: start_range=0, end_range=511 will download first 512 bytes of file.
        :param bool range_get_content_md5:
            When this header is set to True and specified together
            with the Range header, the service returns the MD5 hash for the
            range, as long as the range is less than or equal to 4 MB in size.
        :param progress_callback:
            Callback for progress with signature function(current, total) 
            where current is the number of bytes transfered so far, and total is 
            the size of the file if known.
        :type progress_callback: callback function in format of func(current, total)
        :param int max_connections:
            Set to 1 to download the file sequentially.
            Set to 2 or greater if you want to download a file larger than 64MB in chunks.
            If the file size does not exceed 64MB it will be downloaded in one chunk.
        :param int max_retries:
            Number of times to retry download of file chunk if an error occurs.
        :param int retry_wait:
            Sleep time in secs between retries.
        :param int timeout:
            The timeout parameter is expressed in seconds. This method may make 
            multiple calls to the Azure service and the timeout will apply to 
            each call individually.
        :return: A File with properties and metadata.
        :rtype: :class:`~azure.storage.file.models.File`
        '''
        _validate_not_none('share_name', share_name)
        _validate_not_none('file_name', file_name)
        _validate_not_none('stream', stream)

        if sys.version_info >= (3,) and max_connections > 1 and not stream.seekable():
            raise ValueError(_ERROR_PARALLEL_NOT_SEEKABLE)

        # Only get properties if parallelism will actually be used
        file_size = None
        if max_connections > 1 and range_get_content_md5 is None:
            file = self.get_file_properties(share_name, directory_name, 
                                            file_name, timeout=timeout)
            file_size = file.properties.content_length

            # If file size is large, use parallel download
            if file_size >= self.MAX_SINGLE_GET_SIZE:
                _download_file_chunks(
                    self,
                    share_name,
                    directory_name,
                    file_name,
                    file_size,
                    self.MAX_CHUNK_GET_SIZE,
                    start_range,
                    end_range,
                    stream,
                    max_connections,
                    max_retries,
                    retry_wait,
                    progress_callback, 
                    timeout
                )
                return file

        # If parallelism is off or the file is small, do a single download
        download_size = _get_download_size(start_range, end_range, file_size)
        if progress_callback:
            progress_callback(0, download_size)

        file = self._get_file(
            share_name,
            directory_name,
            file_name,
            start_range=start_range,
            end_range=end_range,
            range_get_content_md5=range_get_content_md5,
            timeout=timeout)

        if file.content is not None:
            stream.write(file.content)

        if progress_callback:
            download_size = len(file.content)
            progress_callback(download_size, download_size)

        file.content = None # Clear file content since output has been written to user stream
        return file