Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Big file upload does not work #206

Closed
AdrianoW opened this issue Jun 10, 2020 · 1 comment
Closed

Big file upload does not work #206

AdrianoW opened this issue Jun 10, 2020 · 1 comment
Labels

Comments

@AdrianoW
Copy link

Hi Folks,

I have used the example provided to upload a big file. It creates the file in Sharepoint but the contents are not filled.

After some debugging, it seemed to me that the upload operation would start but would never be finished.

So I did the following changes to the code:
file upload_session.py
from:

for piece in read_in_chunks(fh, size=self._chunk_size):
    if f_pos == 0:
        upload_result = files.get_by_url(file_name).start_upload(self._upload_id, piece)
    elif f_pos + len(piece) < st.st_size:
        upload_result = files.get_by_url(file_name).continue_upload(self._upload_id, f_pos, piece)
    else:
        self._target_file = files.get_by_url(file_name).finish_upload(self._upload_id, f_pos, piece)
    f_pos += len(piece)

to

for piece in read_in_chunks(fh, size=self._chunk_size): # when the bytes are over, the loop gets out, without calling the else in the previous code
    if f_pos == 0:
        upload_result = files.get_by_url(file_name).start_upload(self._upload_id, piece)
    elif f_pos + len(piece) < st.st_size:
        upload_result = files.get_by_url(file_name).continue_upload(self._upload_id, f_pos, piece)
    f_pos += len(piece)

# in my opinion this should always be called, as this is what tells sharepoint to close the file
self._target_file = files.get_by_url(file_name).finish_upload(self._upload_id, f_pos, piece)
@vgrem
Copy link
Owner

vgrem commented Jun 10, 2020

Greetings!

thanks for spotting this bug, indeed, due to the recent changes to ClientContext, continue_upload among with finish_upload stopped gets triggered, that's the reason why the file gets uploaded as an empty one.

The good news, the latest commit addresses this particular issue (and it is now part of unit tests).

And since it is not yet published into PyPI, the latest version could be grabbed from GitHub:

pip install git+https://github.com/vgrem/Office365-REST-Python-Client.git

Here is an example on how to upload a file via Session API:

def print_upload_progress(offset):
    print("Uploaded '{0}' bytes...".format(offset))   


if __name__ == '__main__':
    ctx = ClientContext.connect_with_credentials(url, UserCredential(username,password))
    size_1Mb = 1000000
    local_path = "../../tests/data/big_buck_bunny.mp4"
    target_url = "/Shared Documents"
    result_file = ctx.web.get_folder_by_server_relative_url(target_url) \
        .files.create_upload_session(local_path, size_1Mb, print_upload_progress)
    ctx.execute_query()
    print('File {0} has been uploaded successfully'.format(result_file.serverRelativeUrl))

@vgrem vgrem closed this as completed Jun 10, 2020
@vgrem vgrem added the bug label Jun 10, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants