Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error 501: when uploading a new file #894

Closed
LuchiLucs opened this issue Sep 10, 2024 · 2 comments
Closed

Error 501: when uploading a new file #894

LuchiLucs opened this issue Sep 10, 2024 · 2 comments
Labels
Status: No Recent Activity status:waiting-for-author-feedback Issue that we've responded but needs author feedback to close type:bug A broken experience

Comments

@LuchiLucs
Copy link

LuchiLucs commented Sep 10, 2024

Describe the bug

I'm trying to replicate the HTTP example with the Python SDK as here:

async with aiofiles.open(src_file_path, 'rb') as file:
    file_content = await file.read()
dest_folder_name = "FolderA"
src_file_name = "FileB.txt"
dest_file_path = f"{dest_folder_name}/{src_file_name}"
drive_item_id = f"/root:/{dest_file_path}:"
uploaded_file = await self.client.drives.by_drive_id(drive_id).items.by_drive_item_id(drive_item_id).content.put(file_content)

But I get a 501 error with message

Cannot upload content to an item representing a folder.

Expected behavior

The file should be uploaded, but I believe there is a problem in the way the drive_item_id is built in the by_drive_item_id method.

How to reproduce

dest_folder_name = "FolderA"
src_file_name = "FileB.txt"
dest_file_path = f"{dest_folder_name}/{src_file_name}"
drive_item_id = f"/root:/{dest_file_path}:"
# drive_item_id example: '/root:/FolderA/FileB.txt:'
uploaded_file = await self.client.drives.by_drive_id(drive_id).items.by_drive_item_id(drive_item_id).content.put(file_content)

SDK Version

No response

Latest version known to work for scenario above?

No response

Known Workarounds

No response

Debug output

Click to expand log ```

msgraph.generated.models.o_data_errors.o_data_error.ODataError:
APIError
Code: 501
message: None
error: MainError(additional_data={}, code='notSupported', details=None, inner_error=InnerError(..., message='Cannot upload content to an item representing a folder.', target=None)

</details>


### Configuration

_No response_

### Other information

_No response_
@LuchiLucs LuchiLucs added status:waiting-for-triage An issue that is yet to be reviewed or assigned type:bug A broken experience labels Sep 10, 2024
@shemogumbe
Copy link
Collaborator

Hello @LuchiLucs thanks for using the SDK and for raising this,

A few pointers in your upload code,
-drive-item - shis should be a DriveItemUploadableProperties object at from msgraph.generated.models.drive_item_uploadable_properties import DriveItemUploadableProperties
Then upload content using PUT,

Optionally use an upload session to upload with a post:

file = open(file_path, 'rb')
        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(
                "drive-id"
            ).items.by_drive_item_id('<destination-address>:'
                                     ).create_upload_session.post(upload_session_request_body)

@shemogumbe shemogumbe added status:waiting-for-author-feedback Issue that we've responded but needs author feedback to close and removed status:waiting-for-triage An issue that is yet to be reviewed or assigned labels Sep 12, 2024

This issue has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for 4 days. It will be closed if no further activity occurs within 3 days of this comment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status: No Recent Activity status:waiting-for-author-feedback Issue that we've responded but needs author feedback to close type:bug A broken experience
Projects
None yet
Development

No branches or pull requests

2 participants