Azure 函数未执行
Azure function doesn't get executed
我目前正在研究 Azure 函数,该函数应通过队列触发器执行。
问题是它不能正常工作。
这是我的 __init__.py
文件:
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
那是我的 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"
}
]
}
那是我的 local.settings.json
:
{
"IsEncrypted": false,
"Values": {
"FUNCTIONS_WORKER_RUNTIME": "python",
"AzureWebJobsStorage": <MyConnectionString>,
"MyStorageAccountConnection": <MyConnectionString>
}
}
我已经为 local.settings.json
中的每个值创建了一个应用程序设置。
当我现在将文件上传到 cmi-input Queue 时,Azure 函数应该实际启动并且文件应该加载到 Blob 容器中(写在代码中)。
但是,没有任何反应。
我是否忘记了任何配置,或者是否有必要 运行 否则?
当我尝试直接在 Visual Studio 代码中启动函数时,我得到了这个:
它卡住了,什么也没有发生,直到我阻止它......
message in the Terminal
感谢您的帮助!
A ServiceBusTrigger
需要形式为
的服务总线连接字符串
Endpoint=sb:// ... SharedAccessKeyName ... SharedAccessKey
如果使用共享访问密钥进行连接。 MyConnectionString
是存储 blob 的存储连接。
您可以在 Azure python example 中看到服务总线连接字符串。
我目前正在研究 Azure 函数,该函数应通过队列触发器执行。 问题是它不能正常工作。
这是我的 __init__.py
文件:
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
那是我的 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"
}
]
}
那是我的 local.settings.json
:
{
"IsEncrypted": false,
"Values": {
"FUNCTIONS_WORKER_RUNTIME": "python",
"AzureWebJobsStorage": <MyConnectionString>,
"MyStorageAccountConnection": <MyConnectionString>
}
}
我已经为 local.settings.json
中的每个值创建了一个应用程序设置。
当我现在将文件上传到 cmi-input Queue 时,Azure 函数应该实际启动并且文件应该加载到 Blob 容器中(写在代码中)。
但是,没有任何反应。
我是否忘记了任何配置,或者是否有必要 运行 否则?
当我尝试直接在 Visual Studio 代码中启动函数时,我得到了这个: 它卡住了,什么也没有发生,直到我阻止它...... message in the Terminal
感谢您的帮助!
A ServiceBusTrigger
需要形式为
Endpoint=sb:// ... SharedAccessKeyName ... SharedAccessKey
如果使用共享访问密钥进行连接。 MyConnectionString
是存储 blob 的存储连接。
您可以在 Azure python example 中看到服务总线连接字符串。