but I get this: TypeError: 'CreateUploadSessionRequestBuilder' object is not callable
so I've changed it to:
\nupload_session = await client.drives.by_drive_id(rds_drive_id).items.by_drive_item_id(f\"root:{destination_full_path}:\").create_upload_session.post()\nbut then I get TypeError: body cannot be null.
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.
\nTo create an upload session, we use the CreateUploadSessionPostRequestBody
\nIt takes DriveItemUploadableProperties
Examples:
\nfrom 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())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
\nResponse:
\nUploadable 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-
|
Based on the suggestion in #40, I've tried to create an upload session like this: but I get this: so I've changed it to: but then I get Can you provide an example on how to properly create an upload session? |
Beta Was this translation helpful? Give feedback.
-
|
Hello @paulschmeida thanks for using the SDK and for asking this. To create an upload session, we use the CreateUploadSessionPostRequestBody 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': 'replace'})
upload_session_request_body = CreateUploadSessionPostRequestBody(
item=uploadable_properties)
print(f"Uploadable Properties: {uploadable_properties.additional_data}")
# can be used for normal drive uploads
try:
upload_session = await user_client.drives.by_drive_id(
"<your-drive-id>"
).items.by_drive_item_id('root:/Project/test_excel_upload.xlsx:'
).create_upload_session.post(
upload_session_request_body)
# print(f"Upload Session URL: {upload_session.upload_url}")
print(dir(upload_session))
# upload_session.put_file(file, destination_path)
print(
f"Upload Session EXPIRATION DATE TIME: {upload_session.expiration_date_time}"
)
print(
f"Upload Session ADDITIONAL DATA: {upload_session.additional_data}"
)
except APIError as ex:
print(f"Error creating upload session: {ex}")
asyncio.run(upload_file())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 Response: |
Beta Was this translation helpful? Give feedback.
Hello @paulschmeida thanks for using the SDK and for asking this.
To create an upload session, we use the CreateUploadSessionPostRequestBody
It takes DriveItemUploadableProperties
Examples: