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

Add support for MemoryFS file-like objects to 'create_upload_session()'

Open pspot2 opened this issue 1 year ago • 1 comments

Hello,

I noticed that with the version 2.5.2 the file office365/sharepoint/internal/queries/upload_session.py was completely removed. I understand the background (create_upload_session() in office365/sharepoint/files/collection.py does essentially the same). However, ironically I was using the create_upload_session_query() method from the removed file because it had one important distinction: PR https://github.com/vgrem/Office365-REST-Python-Client/pull/656 added support for working with file-like objects that do not support standard OS filesystem methods.

In order to add this support back, similar changes need to be applied to create_upload_session(). Specifically, the following small change seems to restore the desired behavior:

diff --git a/office365/sharepoint/files/collection.py b/office365/sharepoint/files/collection.py
index b63456ab..2089b107 100644
--- a/office365/sharepoint/files/collection.py
+++ b/office365/sharepoint/files/collection.py
@@ -60,7 +60,7 @@ class FileCollection(EntityCollection[File]):
         return self.add(file_name, None, True).after_execute(_upload_session)

     def create_upload_session(
-        self, file, chunk_size, chunk_uploaded=None, file_name=None, **kwargs
+        self, file, chunk_size, chunk_uploaded=None, file_name=None, file_size=None, **kwargs
     ):
         # type: (IO|str, int, Callable[[int, ...], None], str, ...) -> File
         """Upload a file as multiple chunks
@@ -68,6 +68,7 @@ class FileCollection(EntityCollection[File]):
         :param int chunk_size: upload chunk size (in bytes)
         :param (long)->None or None chunk_uploaded: uploaded event
         :param str file_name: custom file name
+        :param int file_size: externally-calculated file size
         :param kwargs: arguments to pass to chunk_uploaded function
         """

@@ -76,7 +77,7 @@ class FileCollection(EntityCollection[File]):
             file = open(file, "rb")
             auto_close = True

-        file_size = os.fstat(file.fileno()).st_size
+        file_size = file_size if file_size else os.fstat(file.fileno()).st_size
         file_name = file_name if file_name else os.path.basename(file.name)
         upload_id = str(uuid.uuid4())

This is just a suggestion (I'm not sure how much this exact change will break as the method signature changes). But in general - would you please consider adding this or a similar adjustment to the next release?

Thank you

pspot2 avatar Jan 17 '24 17:01 pspot2

@vgrem may I suggest to add a feature request label? I see no question here 🙃

torzsmokus avatar Aug 15 '25 13:08 torzsmokus