How to create an upload session? #874
-
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.
Replies: 1 comment
-
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: