Skip to content
\n

but I get this: TypeError: 'CreateUploadSessionRequestBuilder' object is not callable

\n

so I've changed it to:

\n
upload_session = await client.drives.by_drive_id(rds_drive_id).items.by_drive_item_id(f\"root:{destination_full_path}:\").create_upload_session.post()\n
\n

but then I get TypeError: body cannot be null.

\n

Can you provide an example on how to properly create an upload session?

","upvoteCount":2,"answerCount":1,"acceptedAnswer":{"@type":"Answer","text":"

Hello @paulschmeida thanks for using the SDK and for asking this.

\n

To create an upload session, we use the CreateUploadSessionPostRequestBody
\nIt takes DriveItemUploadableProperties

\n

Examples:

\n
from msgraph.generated.drives.item.items.item.create_upload_session.create_upload_session_post_request_body import CreateUploadSessionPostRequestBody\nfrom msgraph.generated.models.drive_item_uploadable_properties import DriveItemUploadableProperties\n\ndestination_path = \"my_docs/test_upload_file.txt\"\nfile_path = \"C:/Users/<path-to>/Project_workbook.xlsx\"\nasync def upload_file():\n    uploadable_properties = DriveItemUploadableProperties(\n        additional_data={'@microsoft.graph.conflictBehavior': 'replace'})\n    upload_session_request_body = CreateUploadSessionPostRequestBody(\n        item=uploadable_properties)\n    print(f\"Uploadable Properties: {uploadable_properties.additional_data}\")\n    # can be used for normal drive uploads\n    try:\n\n        upload_session = await user_client.drives.by_drive_id(\n            \"<your-drive-id>\"\n        ).items.by_drive_item_id('root:/Project/test_excel_upload.xlsx:'\n                                 ).create_upload_session.post(\n                                     upload_session_request_body)\n        # print(f\"Upload Session URL: {upload_session.upload_url}\")\n        print(dir(upload_session))\n        # upload_session.put_file(file, destination_path)\n        print(\n            f\"Upload Session EXPIRATION DATE TIME: {upload_session.expiration_date_time}\"\n        )\n        print(\n            f\"Upload Session ADDITIONAL DATA: {upload_session.additional_data}\"\n        )\n\n    except APIError as ex:\n        print(f\"Error creating upload session: {ex}\")\n\n\nasyncio.run(upload_file())
\n

You can then use PUT request on the upload_session following https://learn.microsoft.com/en-us/graph/api/driveitem-createuploadsession?view=graph-rest-1.0

\n

Response:

\n
Uploadable Properties: {'@microsoft.graph.conflictBehavior': 'replace'}\nUpload Session EXPIRATION DATE TIME: 2024-08-26 14:34:26.885000+00:00\nUpload Session ADDITIONAL DATA: {'@odata.context': 'https://graph.microsoft.com/v1.0/$metadata#microsoft.graph.uploadSession'}\n\n
","upvoteCount":1,"url":"https://github.com/microsoftgraph/msgraph-sdk-python/discussions/874#discussioncomment-10452591"}}}
Discussion options

You must be logged in to vote

Hello @paulschmeida thanks for using the SDK and for asking this.

To create an upload session, we use the CreateUploadSessionPostRequestBody
It takes DriveItemUploadableProperties

Examples:

from msgraph.generated.drives.item.items.item.create_upload_session.create_upload_session_post_request_body import CreateUploadSessionPostRequestBody
from msgraph.generated.models.drive_item_uploadable_properties import DriveItemUploadableProperties

destination_path = "my_docs/test_upload_file.txt"
file_path = "C:/Users/<path-to>/Project_workbook.xlsx"
async def upload_file():
    uploadable_properties = DriveItemUploadableProperties(
        additional_data={'@microsoft.graph.conflictBehavior': 'repl…

Replies: 1 comment 3 replies

Comment options

You must be logged in to vote
3 replies
@rubyff
Comment options

@InterStella0
Comment options

@Kallepan
Comment options

Answer selected by shemogumbe
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
documentation Improvements or additions to documentation
5 participants
Converted from issue

This discussion was converted from issue #610 on August 26, 2024 14:20.