VS Code本地开发Azure Functions时触发器异常及调用报错求助
Azure Functions HTTP触发器异常及调用报错问题
过去两天一直通过VS Code本地开发并部署HTTP Trigger类型的Azure Functions,突然出现以下问题:
- 触发器在Azure Portal中消失
- VS Code启动调试时无法在工作区找到该触发器,触发调试报错
- 后续成功部署代码并获取函数URL,但调用时仍出现新报错
初始函数代码
import azure.functions as func from azure.storage.blob import BlobServiceClient from azure.iot.device import IoTHubDeviceClient, Message import crc32c import base64 def main(req: func.HttpRequest) -> func.HttpResponse: # Retrieve the blob name from the HTTP request blob_name = req.params.get('blob_name') if not blob_name: return func.HttpResponse( "Please provide a 'blob_name' parameter in the query string.", status_code=400 ) # Connect to Azure Blob Storage blob_service_client = BlobServiceClient.from_connection_string("blob connection string") blob_client = blob_service_client.get_blob_client(container="blob-device", blob=blob_name) try: # Download the blob blob_data = blob_client.download_blob() binary_data = blob_data.readall() # Send the binary file to the device (pseudo-code) send_to_device(binary_data) return func.HttpResponse("Binary file sent to the device successfully.", status_code=200) except Exception as e: return func.HttpResponse(f"An error occurred: {str(e)}", status_code=500) def send_to_device(binary_data): # Calculate CRC32 checksum of the binary data crc32_checksum = crc32c.crc32(binary_data) # Convert binary data to base64 for transmission # encoded_data = base64.b64encode(binary_data) # Connect to IoT Hub conn_str = "Iot Hub connect string" device_client = IoTHubDeviceClient.create_from_connection_string(conn_str) try: # Connect to the IoT Hub device_client.connect() # Send the binary file along with CRC32 checksum as a message to IoT Hub msg = { "file": binary_data, "checksum": crc32_checksum } device_client.send_message(msg) print("Binary file sent to IoT Hub successfully.") # Receive acknowledgment or any other messages from IoT Hub while True: message = device_client.receive_message() if message: print("Received message from IoT Hub:") print(message.data) # Handle the received message here # Example: If the message contains acknowledgment or status # handle_message(message) else: print("No more messages from IoT Hub.") break except Exception as e: print(f"An error occurred while sending data to IoT Hub: {e}") finally: # Disconnect from IoT Hub device_client.disconnect()
更新后函数代码
import azure.functions as func import logging from azure.storage.blob import BlobServiceClient from azure.iot.device import IoTHubDeviceClient, Message import crc32c import base64 import os import json import random import sys from azure.iot.hub import IoTHubRegistryManager app = func.FunctionApp(http_auth_level=func.AuthLevel.ANONYMOUS) @app.route(route="http_trigger") def http_trigger(req: func.HttpRequest) -> func.HttpResponse: logging.info('Python HTTP trigger function processed a request.') # Retrieve the blob name from the HTTP request blob_name = req.params.get('blob_name') if not blob_name: return func.HttpResponse( "Please provide a 'blob_name' parameter in the query string.", status_code=400 ) # Connect to Azure Blob Storage blob_service_client = BlobServiceClient.from_connection_string("DefaultEndpointsProtocol=https;AccountName=otastorage;AccountKey=5dCDeZPLUtEb5ucDZbJT8gosEB3P6LK92D4HRW244SnUU1xsgWDTfClJNVhLDonZ9cChAa8phO8+AStvBUT/w==;EndpointSuffix=core.windows.net") blob_client = blob_service_client.get_blob_client(container="blob-device", blob=blob_name) try: # Download the blob blob_data = blob_client.download_blob() binary_data = blob_data.readall() # Send the binary file to the device (pseudo-code) send_to_device(binary_data) return func.HttpResponse("Binary file sent to the device successfully.", status_code=200) except Exception as e: return func.HttpResponse(f"An error occurred: {str(e)}", status_code=500) def send_to_device(binary_data): # Calculate CRC32 checksum of the binary data crc32_checksum = crc32c.crc32(binary_data) # Convert binary data to base64 for transmission # encoded_data = base64.b64encode(binary_data) # Connect to IoT Hub CONNECTION_STRING = "HostName=iothub.azure-devices.net;SharedAccessKeyName=service;SharedAccessKey=MmjcbQ0RCmuIK/JfGO28odyxCf9ZN7TL8AIoTHrJNvA=" DEVICE_ID = "OTA-Device" try: # Create IoTHubRegistryManager registry_manager = IoTHubRegistryManager(CONNECTION_STRING) '''props={} # optional: assign system properties props.update(messageId = "message_%d" % i) props.update(correlationId = "correlation_%d" % i) props.update(contentType = "application/json")''' msg_body = { "file": base64.b64encode(binary_data).decode(), "checksum": crc32_checksum } msg = Message(json.dumps(msg_body)) registry_manager.send_c2d_message(DEVICE_ID, msg) except Exception as ex: print ( "Unexpected error {0}" .format(ex) ) return
设备端接收代码
import time from azure.iot.device import IoTHubDeviceClient RECEIVED_MESSAGES = 0 CONNECTION_STRING = "HostName=iothub.azure-devices.net;DeviceId=MCU;SharedAccessKey=pOkHxKaDejoJUSgUU3ZdmKqG/KV3XOcbgILSX8tDiMk=" def message_handler(message): global RECEIVED_MESSAGES RECEIVED_MESSAGES += 1 print("") print("Message received:") # print data from both system and application (custom) properties for property in vars(message).items(): print (" {}".format(property)) print("Total calls received: {}".format(RECEIVED_MESSAGES)) def main(): print ("Starting the Python IoT Hub C2D Messaging device sample...") # Instantiate the client client = IoTHubDeviceClient.create_from_connection_string(CONNECTION_STRING, websockets=True) print ("Waiting for C2D messages, press Ctrl-C to exit") try: # Attach the handler to the client client.on_message_received = message_handler while True: time.sleep(1000) except KeyboardInterrupt: print("IoT Hub C2D Messaging device sample stopped") finally: # Graceful exit print("Shutting down IoT Hub Client") client.shutdown() if __name__ == '__main__': main()
内容的提问来源于stack exchange,提问作者sai
相关产品推荐
相关产品推荐

