Search code examples
pythonazuremicrosoft-graph-apionedrivemicrosoft-graph-sdks

Upload a file using msgraph Python SDK


I wanted to recreate the HTTP upload file example from the documentation:

async with aiofiles.open(src_file_path, 'rb') as file:
    file_content = await file.read()

dest_folder_name = "FolderA"
src_file_name = "FileB.txt"
dest_file_path = f"{dest_folder_name}/{src_file_name}"
drive_item_id = f"/root:/{dest_file_path}:"
uploaded_file = await self.client.drives.by_drive_id(drive_id).items.by_drive_item_id(drive_item_id).content.put(file_content)

But I get a 501 error:

msgraph.generated.models.o_data_errors.o_data_error.ODataError: 
         APIError
         Code: 501
         message: None
         error: MainError(additional_data={}, code='notSupported', details=None, inner_error=InnerError(...,
message='Cannot upload content to an item representing a folder.',
target=None)

I believe the problem is in how I create the variable drive_item_id.


Solution

  • It should be easy to fix by removing the slash / from the beginning of drive_item_id

    change this

    drive_item_id = f"/root:/{dest_file_path}:"
    

    to

    drive_item_id = f"root:/{dest_file_path}:"