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