Search code examples
pythonmicrosoft-graph-api

Microsoft graph, upload a large file (> 100 MB) with the Python SDK


I want to upload large file to a Sharepoint folder in Python. I'm using the msgraph-sdk-python.

I found an example of what I want to do on the microsoft graph documentation but not in Python.

On this link, there is an example in PHP, it's really close to what I can do in Python. But I can't seem to get my hands on a class like LargeFileUploadTask().

Also I have found a Python example of upload session (on the calendar section) in python but I dont understand how I'm supposed to send my file with this technic.

A similar issue has been opened on the project Github repo and a work around for this problem is proposed. But no real solution to this problem.

I understand that I need to create an UploadSession object and use it like in the PHP snippet but I really don't understand how to do it.

This is the code I use, based on the work around on the github issue, but the upload is really slow

async def _upload_video(self, video: Video) -> None:
    try:
        if not video.is_downloaded or not video.is_processed:
            raise Exception("Video not downloaded or not processed")

        print(f"Uploading video {video.title}")

        request_body = DriveItem()
        request_body.name = video.title
        request_body.file = File()
        result = await self.app_client.drives.by_drive_id(self.drive_id).items.by_drive_item_id(
            self.trans_video_id).children.post(body=request_body)

        with open(video.path, "rb") as f:
            data = f.read()
        print(f"Uploading data to {video.title}")
        await self.app_client.drives.by_drive_id(self.drive_id).items.by_drive_item_id(result.id).content.put(
            body=data)
        print(f"Video {video.title} has been uploaded")
    except Exception as e:
        print(e)


Solution

  • Seems like LargeFileUploadTask will be added in the next release, you can check the PR here, it includes an example on how to use it.