相关文章推荐
叛逆的太阳  ·  Android ...·  2 周前    · 
俊逸的青蛙  ·  使用 IIS 8.5 ...·  2 周前    · 
帅呆的玉米  ·  JMeter: ...·  1 月前    · 
英姿勃勃的葡萄  ·  SQL ...·  7 月前    · 
睡不着的橡皮擦  ·  Java reflection: ...·  9 月前    · 
另类的蚂蚁  ·  blockchain - Python ...·  1 年前    · 

Hi, I am trying to upload a file from a local laptop (Windows 10, python 3.8.5), but the file cannot be uploaded somehow.

I just copied and pasted the following official source code and executed.

https://learn.microsoft.com/en-us/azure/iot-hub/file-upload-python

As the title mentioned, the appearing error is " TypeError: 'coroutine' object is not subscriptable " .

Since I continuously received warnings saying " RuntimeWarning: coroutine 'execute_patch_for_async.<locals>.connect' was never awaited device_client.connect() ", I believe it is something related to asyncio library.

Any solutions?

@jyano Welcome to Microsoft Q&A forum!

Could you confirm if you have made any changes to the existing code copied from the documentation?

The warning message "RuntimeWarning: coroutine 'execute_patch_for_async..connect' was never awaited device_client.connect()" suggests that you are not properly awaiting the connect() method of the device_client object.

Thank you for the answer.

I just copied and pasted without modifying anything.

I wonder why the warnings and the coroutine error comes, alithough the Asyncio library is not used in the program at all. Therefore, I cannot use "await" function either in this program.

Any other solutions?

Where I am stuck is the following part( -> blob_info["hostName"] ):

(The blob_info is the Coroutine object somehow.)

def store_blob(blob_info, file_name):
        sas_url = "https://{}/{}/{}{}".format(
            blob_info["hostName"],
            blob_info["containerName"],
            blob_info["blobName"],
            blob_info["sasToken"]
			 

It actually worked after chaging the original code as follows (the rest is the same):

async def run_sample(device_client): # Connect the client await device_client.connect() # Get the storage info for the blob blob_name = os.path.basename(PATH_TO_FILE) storage_info = await device_client.get_storage_info_for_blob(blob_name) # Upload to blob success, result = store_blob(storage_info, PATH_TO_FILE) if success == True: print("Upload succeeded. Result is: \n") print(result) print() await device_client.notify_blob_upload_status( storage_info["correlationId"], True, 200, "OK: {}".format(PATH_TO_FILE) else: # If the upload was not successful, the result is the exception object print("Upload failed. Exception is: \n") print(result) print() await device_client.notify_blob_upload_status( storage_info["correlationId"], False, result.status_code, str(result) async def main(): device_client = IoTHubDeviceClient.create_from_connection_string(CONNECTION_STRING) print("IoT Hub file upload sample, press Ctrl-C to exit") await run_sample(device_client) except KeyboardInterrupt: print("IoTHubDeviceClient sample stopped") finally: # Graceful exit await device_client.shutdown() if __name__ == "__main__": loop = asyncio.get_event_loop() loop.run_until_complete(main()) finally: loop.close() -cannot delete the following scripts, but please ignore it-- print("IoT Hub file upload sample, press Ctrl-C to exit") await run_sample(device_client) except KeyboardInterrupt: print("IoTHubDeviceClient sample stopped") finally: # Graceful exit await device_client.shutdown() if __name__ == "__main__": loop = asyncio.get_event_loop() loop.run_until_complete(main()) finally: loop.close()