You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When uploading a DriveItem, an error occurs with the message: '[item] Read called with an open stream or textreader. Please close any open streams or text'readers before calling Read.'
#276
Closed
hide212131 opened this issue
Jul 26, 2023
· 5 comments
I am trying to upload content to SharePoint.
I can handle authentication, obtain Drive information, and download content successfully.
However, I cannot upload content. I encountered the following error:
msgraph.generated.models.o_data_errors.o_data_error.ODataError: {"message": null, "response_status_code": 400,
...
"message": "[item] Read called with an open stream or textreader. Please close any open streams or text readers before calling Read."
I attempted to pass a stream and made the necessary modifications as follows. The error no longer occurs; however, the uploaded file has a size of 0 bytes.
request_body.content=io.BytesIO(b'abcde')
Could the issue be due to incorrect usage of the API?
The text was updated successfully, but these errors were encountered:
After tinkering for a several days, I found the following code as a workaround:
# Create an empty file called a.txt via Post API
request_body = DriveItem()
request_body.name = 'a.txt'
request_body.file = File()
result = await drive_req.items.by_drive_item_id('root').children.post(body=request_body)
# Put actual data using Put API
file_id = result.id
content = b'abcde' # Load your actual data here
await drive_req.items.by_drive_item_id(file_id).content.put(body=content)
So instead of trying to put bytes data using POST, you can upload data with PUT after creating an empty DriveItem.
@shinkawk, thank you for the example. I was able to successfully upload a file another way using the example below, but that has downsides (it allows to overwrite existing files, creating a version of the previous file, without providing additional request params.
Given MSGraph documentation, both ways should work for files <4MB. Anything larger would necessitate creating an upload session which is also not documented. If anyone has a sample of creating a session to upload larger files that will be greatly appreciated.
I am trying to upload content to SharePoint.
I can handle authentication, obtain Drive information, and download content successfully.
However, I cannot upload content. I encountered the following error:
I attempted to pass a stream and made the necessary modifications as follows. The error no longer occurs; however, the uploaded file has a size of 0 bytes.
Could the issue be due to incorrect usage of the API?
The text was updated successfully, but these errors were encountered: