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()