An error occurs "TypeError: 'coroutine' object is not subscriptable" while trying to upload a file via IoT HUB

jyano 0 Reputation points
2023-05-16T02:28:09.3866667+00:00

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?

Azure Blob Storage
Azure Blob Storage
An Azure service that stores unstructured data in the cloud as blobs.
1,696 questions
Azure IoT Hub
Azure IoT Hub
An Azure service that enables bidirectional communication between internet of things (IoT) devices and applications.
799 questions
{count} votes

1 answer

Sort by: Most helpful
  1. jyano 0 Reputation points
    2023-05-17T09:10:03.31+00:00

    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)
    
        try:
            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()
        try:
            loop.run_until_complete(main())
        finally:
            loop.close()
        
    -cannot delete the following scripts, but please ignore it--
        try:
            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()
        try:
            loop.run_until_complete(main())
        finally:
            loop.close()