我目前正在研究一個應該通過佇列觸發器執行的 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 佇列時,Azure 函式應該實際啟動,并且檔案應該加載到 Blob 容器中(寫在代碼中)。
然而,什么也沒有發生。我是否忘記了任何配置,或者是否有必要以其他方式運行它們?
當我嘗試直接在 Visual Studio Code 中啟動該函式時,我只是得到了這個:它卡住了,直到我停止它之前什么都沒有發生...... 終端中的訊息
謝謝你的幫助!
uj5u.com熱心網友回復:
AServiceBusTrigger需要表單的服務總線連接字串
Endpoint=sb:// ... SharedAccessKeyName ... SharedAccessKey
如果使用共享訪問密鑰進行連接。MyConnectionString是用于存盤 blob 的存盤連接。
您可以在Azure python 示例中看到服務總線連接字串。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/327750.html
下一篇:python中的布林值函式
