Office365-REST-Python-Client icon indicating copy to clipboard operation
Office365-REST-Python-Client copied to clipboard

Upload large file from memory or using content, instead of local path

Open massettim opened this issue 3 years ago • 1 comments

Hello,

I'm using your library to upload files to Sharepoint, however, I need to save the in-memory content in the Sharepoint folder.

From your example, I've already tested that for single upload is working super well, but is limited to 250 MB https://github.com/vgrem/Office365-REST-Python-Client/blob/31080ae2a9c152dcc9d1b4ce04ff2547876eca79/examples/sharepoint/files/upload_file.py

The second method you propose requires the file to be locally stored, which is not my case. https://github.com/vgrem/Office365-REST-Python-Client/blob/31080ae2a9c152dcc9d1b4ce04ff2547876eca79/examples/sharepoint/files/upload_large_file.py

Temporary, I have reworked the logic and created a session myself using the UploadSessionQuery class but it is gruesome. I had to pass manually the _file_handle here https://github.com/vgrem/Office365-REST-Python-Client/blob/31080ae2a9c152dcc9d1b4ce04ff2547876eca79/office365/sharepoint/internal/queries/upload_session.py#L29 and get rid of the file_size to make it work.

Do you have in plan to update the method for content or file-like object?

massettim avatar May 20 '22 10:05 massettim

Hi @vgrem , I am looking for similar logic to upload file in chunks based on file content instead of passing local path as my files are in cloud. Kindly let me know if there is any latest code release for this request to upload file using file content method instead of local path ?

samrackson avatar Aug 25 '22 03:08 samrackson

Greetings,

starting from 2.3.15 version it is (finally) supported to pass file object into create_upload_session method:

def print_upload_progress(offset):
    file_size = os.path.getsize(local_path)
    print("Uploaded '{0}' bytes from '{1}'...[{2}%]".format(offset, file_size, round(offset / file_size * 100, 2)))


with open(local_path, 'rb') as f:
    uploaded_file = target_folder.files.create_upload_session(f, size_chunk,
                                                              print_upload_progress).execute_query()

Example: upload_large_file.py

/cc @massettim @samrackson, thank you for raising the attention to this improvement, gruesome UploadSessionQuery class gone for good after a few refactoring ;) And apology it took longer than originally expected.

vgrem avatar Oct 28 '22 18:10 vgrem