1

I am sending a multipart file from my client to my graphql server written in Python. The file gets received as a

<starlette.datastructures.UploadFile object>

Now when I try to upload the file to Azure storage using the upload_blob method I get this error expected str, bytes or os.PathLike object, not UploadFile

I tried reading the contents of the file using the file.read() method as mentioned in starlette docs. This is covered in this link at the very bottom https://www.starlette.io/requests/.

Here is my python code that does all of the above -

@mutation.field("fileUpload")
async def resolve_fileUpload(_, info, file):
    print(f"File - {file.filename}")
    bytes_file = await file.read()
    container_client = blob_service_client.get_container_client(
        '4160000000')
    if not container_client.exists():
        container_client.create_container()

    with open(file, "rb") as file:
        result = container_client.upload_blob(
            name='avatar', data=bytes_file)
    return {
        "status": 200,
        "error": "",
        "fileUrl": "www.test.com"
    }

Please advise. Thankyou.

June 7th 2023 - Added Stacktrace

[2023-06-07T16:30:47.294Z] expected str, bytes or os.PathLike object, not UploadFile

GraphQL request:3:3
2 |   __typename
3 |   fileUpload(file: $file) {
  |   ^
4 |     __typename
Traceback (most recent call last):
  File "C:\Users\sumch\AppData\Local\Programs\Python\Python310\lib\site-packages\graphql\execution\execute.py", line 528, in await_result
    return_type, field_nodes, info, path, await result
  File "C:\Users\sumch\OneDrive\Projects\Flutter Projects\bol\bol-api\bol-api\user_operations\mutations.py", line 86, in resolve_fileUpload        
    with open(file, "rb") as file:
TypeError: expected str, bytes or os.PathLike object, not UploadFile

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\sumch\AppData\Local\Programs\Python\Python310\lib\site-packages\graphql\execution\execute.py", line 1036, in await_result
    return build_response(await result, errors)  # type: ignore
  File "C:\Users\sumch\AppData\Local\Programs\Python\Python310\lib\site-packages\graphql\execution\execute.py", line 403, in set_result
    results[response_name] = await result
  File "C:\Users\sumch\AppData\Local\Programs\Python\Python310\lib\site-packages\graphql\execution\execute.py", line 535, in await_result
    self.handle_field_error(error, return_type)
  File "C:\Users\sumch\AppData\Local\Programs\Python\Python310\lib\site-packages\graphql\execution\execute.py", line 569, in handle_field_error    
    raise error
  File "C:\Users\sumch\AppData\Local\Programs\Python\Python310\lib\site-packages\graphql\execution\execute.py", line 528, in await_result
    return_type, field_nodes, info, path, await result
  File "C:\Users\sumch\OneDrive\Projects\Flutter Projects\bol\bol-api\bol-api\user_operations\mutations.py", line 86, in resolve_fileUpload        
    with open(file, "rb") as file:
graphql.error.graphql_error.GraphQLError: expected str, bytes or os.PathLike object, not UploadFile

GraphQL request:3:3
2 |   __typename
3 |   fileUpload(file: $file) {
  |   ^
4 |     __typename
Sumchans
  • 3,088
  • 6
  • 32
  • 59
  • I think you made a mistake, you should have `data=file.read()` – C.Nivs Jun 06 '23 at 05:50
  • @C.Nivs That didn't work. I get the same error. – Sumchans Jun 07 '23 at 01:23
  • Can you include the entire stacktrace please? – C.Nivs Jun 07 '23 at 13:52
  • @C.Nivs stacktrace has been added to the original post. – Sumchans Jun 07 '23 at 16:32
  • You are passing a `fileUpload` object rather than a `pathlib.Path`, file object, or `str`. sounds like you need `with open(file.filename, 'rb') as fh:` – C.Nivs Jun 07 '23 at 17:19
  • I have tried ```with open(file['filename'], 'rb) as file:``` & ```with open(file.filename, 'rb) as file:```. For the first one I get UploadFile is not subscriptable and for the second one I get No such file or directory – Sumchans Jun 07 '23 at 17:39
  • Then you don't have it saved locally. You don't need the `with open` context manager. You need to get the actual result from `file.read()` – C.Nivs Jun 07 '23 at 18:45
  • Just to make it clear the code here is a graphql server written in python and will be running in Azure functions. The file that this server receives is from an app running on a mobile device. – Sumchans Jun 07 '23 at 18:58
  • As per your comment should we be doing ``` variable = file.read()``` and then use the variable inside the ```with open```. As that what you meant? This has been tried and this is what you see in the code above. – Sumchans Jun 07 '23 at 19:01
  • @C.Nivs Would you be able to add an example code for me to verify? – Sumchans Jun 08 '23 at 18:58
  • 1
    You might find [this answer](https://stackoverflow.com/a/70653605/17865804), as well as [this answer](https://stackoverflow.com/a/70665801/17865804) and [this answer](https://stackoverflow.com/a/73815575/17865804) helpful – Chris Jun 09 '23 at 08:20
  • Thankyou @Chris – Sumchans Jun 10 '23 at 02:20

0 Answers0