Skip to content

Error 501: when uploading a new file #894

Closed

Description

@LuchiLucs

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_

Activity

  1. shemogumbe commented on Sep 12, 2024

    @shemogumbe
    Contributor

    Hello Luca (@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)
  2. microsoft-github-policy-service commented on Sep 16, 2024

    @microsoft-github-policy-service

    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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions