Repository navigation
Closed
Labels
Description
Describe the bug
I am trying to upload a PDF file to OneDrive, but I receive a 400 APIError with an Invalid Request.
Expected behavior
Successful upload of the file.
How to reproduce
files = [item.name for item in await self._get_onedrive_items(
query_filter="file ne null and file/mimeType eq 'application/pdf'"
)]
folder_id_map = await self._get_folder_ids(source_folders)
for folder in source_folders:
folder_path = os.path.join('documents', folder)
if not os.path.exists(folder_path):
continue
for file_name in os.listdir(folder_path):
if file_name in files:
continue
file_path = os.path.join(folder_path, file_name)
with open(file_path, 'rb') as file:
file_content = file.read()
folder_id = folder_id_map.get(folder)
if not folder_id:
print(f"Folder ID for '{folder}' not found.")
continue
parent_reference = ItemReference(
id=folder_id
)
drive_item = DriveItem(
name=file_name,
file=File(),
parent_reference=parent_reference,
content=file_content
)
try:
response = await self._user_client.drives.by_drive_id(
os.getenv('ONEDRIVE_DEV_DRIVE_ID')
).items.post(drive_item)
print("Response", response)
except Exception as e:
print(f"Failed to upload file '{file_name}': {e}")SDK Version
msgraph-core~=1.1.2 msgraph-sdk~=1.5.3
Latest version known to work for scenario above?
No response
Known Workarounds
No response
Debug output
Click to expand log
```APIError
Code: 400
message: None
error: MainError(additional_data={}, code='invalidRequest', details=None, inner_error=InnerError(additional_data={}, client_request_id='13230f64-2c2e-4083-93c1-f7325291333c', date=DateTime(2024, 8, 22, 10, 12, 34, tzinfo=Timezone('UTC')), odata_type=None, request_id='f1815677-f808-46dd-af6d-7882803e975b'), message='Invalid request', target=None)
</details>
### Configuration
- OS: Windows
- architecture: AMD64
### Other information
The 400 APIError could be more robust than just "Invalid request"
Activity
shemogumbe commentedon Aug 22, 2024
Hello Mircea Talu (@Mirciulica15) thanks for using the SDK and for reporting this,
How large is the file you want to upload, do you want to do it in chunks:
If so, use the LargeFileUploadTask from https://github.com/microsoftgraph/msgraph-sdk-python-core/
Optionally, on your snippet, you can use https://learn.microsoft.com/en-us/graph/api/driveitem-createuploadsession?view=graph-rest-1.0
Mirciulica15 commentedon Aug 22, 2024
Thank you for the reply, Shem! I managed to do it using the large file upload method 😀
Do you know whether it is possible to place the files in a specific folder within the drive? I tried to do that with an ItemReference, but it did not work out.
I would like to make a PR with my implementation in the examples directory later if that's ok, in case other people have the same use case and need help.
TwE3d commentedon Nov 17, 2024
For anyone stumbling across this issue.
You can specifiy the folder in the following line:
It will create the folder, if necessary.
crispycookies commentedon Dec 17, 2024
Did anything change in the meantime? I tried the very example given by Shem Ogumbe (@shemogumbe) and it fails with:
I try to upload a
PDFto sharepoint. Obviously, I replaced the drive ID and upload to a different path:root:/TEST/test_upload.pdf:On top of that, I use theGraphServiceClientfor user_client.I would be very grateful if someone could point me to the right direction.
Mirciulica15 commentedon Dec 18, 2024
I know this might seem like a weird question, but have you checked if the file was successfully uploaded? I remember that at some point I was also receiving 400, but I could see the file successfully uploaded to SharePoint.
crispycookies commentedon Dec 18, 2024
Mircea Talu (@Mirciulica15) you are, indeed, correct: They were uploaded... Thank you!