I'm currently working on an Azure function which should get executed through an Queue-Trigger. The problem is that it doesn't work as it should.
This is my __init__.py
file:
all imports
...
def main(req: func.QueueMessage, res: func.Out[str]) -> func.QueueMessage:
print(req)
cmi_guid = req.get_body().decode('utf-8')
logging.info('Received message: %s', req.get_body().decode('utf-8'))
logging.info('Received message FULL: %s', req)
tempFilePath = tempfile.gettempdir()
print(tempFilePath)
infile = cmi_guid '.xlsx'
outfile = cmi_guid '_output.xlsx'
local_in_file_path = os.path.join(tempFilePath, infile)
local_out_file_path = os.path.join(tempFilePath, outfile)
logging.info('Got temp file path: %s', tempFilePath)
delete_files(tempFilePath, ".xlsx")
logging.info('Deleted xlsx files in temp directory')
blob_service_client = BlobServiceClient.from_connection_string(<MyConnectionString>)
container_name = "cmi"
# Create a blob client using the local file name as the name for the blob
blob_client = blob_service_client.get_blob_client(container=container_name, blob=infile)
with open(local_in_file_path, "wb") as download_file:
download_file.write(blob_client.download_blob().readall())
logging.info('Received input file from blob')
df_out = start_forecast(local_in_file_path)
with pd.ExcelWriter(local_out_file_path, engine='xlsxwriter') as writer:
df_out.to_excel(writer, sheet_name='Forecast', index=False)
blob_client = blob_service_client.get_blob_client(container=container_name, blob=outfile)
try:
with open(local_out_file_path, "rb") as data:
blob_client.upload_blob(data)
except Exception as e:
logging.info('Exception when uploading blob: %s', e)
logging.info('Done uploading blob to storage')
# res.set(cmi_guid)
return cmi_guid
That is my function.json
:
{
"scriptFile": "__init__.py",
"bindings": [
{
"name": "req",
"type": "queueTrigger",
"direction": "in",
"queueName": "cmi-input",
"connection": "AzureWebJobsStorage"
},
{
"name": "res",
"type": "queue",
"direction": "out",
"queueName": "cmi-output",
"connection": "AzureWebJobsStorage"
}
]
}
Thats my local.settings.json
:
{
"IsEncrypted": false,
"Values": {
"FUNCTIONS_WORKER_RUNTIME": "python",
"AzureWebJobsStorage": <MyConnectionString>,
"MyStorageAccountConnection": <MyConnectionString>
}
}
I have created for every value in the local.settings.json
an application setting.
When I now upload an file to the cmi-input Queue then the Azure function should actually be started and a file should be loaded into a Blob container (is written in the code).
However, nothing happens.
Have I forgotten any configurations or is it necessary to run them otherwise?
When I'm trying to start the function directly in Visual Studio Code I'm just getting this: it gets stuck and nothing happens anymore until I stop it... message in the Terminal
Thanks for your help!
CodePudding user response:
A ServiceBusTrigger
needs a Service Bus connection string of the form
Endpoint=sb:// ... SharedAccessKeyName ... SharedAccessKey
if using shared access keys to connect. MyConnectionString
is the connection to storage for storing the blob.
You can see the Service Bus connection string in the Azure python example.