def upload_file_to_sp(self, file_path, *remote_path):
        folder_url = "/".join(["Shared Documents", "beta_builds"] +
                              list(remote_path))
        target_folder = self.ctx.web.ensure_folder_path(folder_url)

        size_chunk = 100 * 1024 * 1024  # MB
        file_size = os.path.getsize(file_path)

        if file_size > size_chunk:
            result_file = target_folder.files.create_upload_session(
                file_path, size_chunk, self.print_upload_progress, file_size)
        else:
            with open(file_path, 'rb') as content_file:
                file_content = content_file.read()
            name = os.path.basename(file_path)
            result_file = target_folder.upload_file(name, file_content)
        self.ctx.execute_query()

        request = RequestOptions(
            r"{0}web/getFileByServerRelativeUrl('{1}')/\$value".format(
                self.ctx.service_root_url(), result_file.serverRelativeUrl))
        request.stream = True
        response = self.ctx.execute_request_direct(request)
        remote_size = int(response.headers['Content-Length'])
        if abs(file_size - remote_size) > 0.05 * file_size:
            raise UploaderError("File size difference is more than 5%")

        logging.info('File {0} has been uploaded successfully'.format(
            result_file.serverRelativeUrl))
        return folder_url
def download_file(context,
                  download_url,
                  file_object,
                  chunk_downloaded=None,
                  chunk_size=1024 * 1024):
    """

    :type context: office365.sharepoint.client_context.ClientContext
    :type download_url: str
    :type file_object: typing.IO
    :type chunk_downloaded: (int)->None or None
    :type chunk_size: int
    """

    request = RequestOptions(
        r"{0}web/getFileByServerRelativeUrl('{1}')/\$value".format(
            ctx.service_root_url(), download_url))
    request.stream = True
    response = context.execute_request_direct(request)
    response.raise_for_status()
    bytes_read = 0
    for chunk in response.iter_content(chunk_size=chunk_size):
        bytes_read += len(chunk)
        if callable(chunk_downloaded):
            chunk_downloaded(bytes_read)
        file_object.write(chunk)
 def _download_inner():
     request = RequestOptions(
         r"{0}web/getFileByServerRelativeUrl('{1}')/\$value".format(
             self.context.service_root_url(), self.serverRelativeUrl))
     request.stream = True
     response = self.context.execute_request_direct(request)
     response.raise_for_status()
     bytes_read = 0
     for chunk in response.iter_content(chunk_size=chunk_size):
         bytes_read += len(chunk)
         if callable(chunk_downloaded):
             chunk_downloaded(bytes_read)
         file_object.write(chunk)