pythonazureazure-storage-queues

Error adding item to Azure Storage Queue from Python


Have a (so far) very simple Azure Function in Python. Trying to use Azure Queues for the first time. Modeling off of the tutorial here. When I try to run this I get the following error:

System.Private.CoreLib: Exception while executing function: Functions.applyNewSurvey123. System.Private.CoreLib: Result: Failure Exception: TypeError: unable to encode outgoing TypedData: unsupported type "<class 'azure_functions_worker.bindings.generic.GenericBinding'>" for Python type "list" Stack: File "C:\Program Files\Microsoft\Azure Functions Core Tools\workers\python\3.9/WINDOWS/X64\azure_functions_worker\dispatcher.py", line 425, in _handle__invocation_request param_binding = bindings.to_outgoing_param_binding( File "C:\Program Files\Microsoft\Azure Functions Core Tools\workers\python\3.9/WINDOWS/X64\azure_functions_worker\bindings\meta.py", line 160, in to_outgoing_param_binding datum = get_datum(binding, obj, pytype) File "C:\Program Files\Microsoft\Azure Functions Core Tools\workers\python\3.9/WINDOWS/X64\azure_functions_worker\bindings\meta.py", line 108, in get_datum raise TypeError( .

My code:

import logging
import azure.functions as func

def main(req: func.HttpRequest, 
         msg: func.Out[str]) -> func.HttpResponse:
    jsonString = req.get_json()

    try:
        msg.set(jsonString)
        return func.HttpResponse("HTTP triggered successfully.", status_code=200)
    except Exception as e:
        logging.error(f"Error: {e}")
        return func.HttpResponse("NOPE", status_code=500)

I've got an Azure Storage Queue set up and function.json seems to be set up correctly. If I set msg to just some plain text, it gives the same error. If I set a logging.error message after msg.set(), I do see the message. Any ideas what I'm doing wrong here?


Solution

  • I have reproduced in my environment and got expected results as below and I have followed Micrsoft-Documnet and below is the python code which worked for me:

    from azure.storage.queue import QueueClient
    qc = QueueClient.from_connection_string(conn_str="DefaultEndpointsProtocol=https;AccountName=rithwikstore;AccountKey=voop0PvnAUcSHWrWd6WLmNkHpA64RWNPol9OWTTXI5DbtEE/y8M113JlJATzn24LkzeNveTeOHS3+AStQIsUZg==;EndpointSuffix=core.windows.net", queue_name="rithwik")
    msg = "Hello Rithwik"
    qc.send_message(msg)
    

    enter image description here

    Here you should give your queue name (queue_name) and connection string (conn_str) of Storage account.

    Output:

    enter image description here