将 python 函数应用程序部署到 azure

问题描述 投票:0回答:1

我正在尝试制作我的第一个 python 函数应用程序。该应用程序只是一个创建 blob 文件的简单应用程序。代码在本地运行。但是,当我部署到 Azure 并且包含注释掉的代码时,我没有收到错误,但该函数未在 azure 门户中加载(未显示)。

enter image description here

如果我按原样部署代码,那么它就可以工作。我只是使用直接部署到 azure web 应用程序的 azure 扩展。您需要进行一些 pip 安装或其他操作吗?

import logging
# import datetime
import azure.functions as func

from azure.identity import DefaultAzureCredential
from azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient

app = func.FunctionApp()

# def upload_blob_data(blob_service_client: BlobServiceClient, container_name: str):

#     time_now  = datetime.datetime.now().strftime('%m_%d_%Y_%H_%M_%S') 
#     blob_client = blob_service_client.get_blob_client(container=container_name, blob=f"{time_now}.txt")
#     data = b"Sample data for blob"

#     # Upload the blob data - default blob type is BlockBlob
    
#     blob_client.upload_blob(data, blob_type="BlockBlob")

@app.timer_trigger(schedule="0 * * * * *", arg_name="myTimer", run_on_startup=False, use_monitor=False) 
def timer_trigger(myTimer: func.TimerRequest) -> None:

    # account_url = "https://xx.blob.core.windows.net"
    # credential = DefaultAzureCredential()

    # # Create the BlobServiceClient object
    # blob_service_client = BlobServiceClient(account_url, credential=credential)

    # upload_blob_data(blob_service_client, "json")

    logging.info('Python timer trigger function executed.')
python azure azure-functions
1个回答
0
投票

我已经创建了一个Python Azure函数应用程序并部署了您的函数代码。能够在门户中同步计时器触发器。

function_app.py:

import datetime
import logging
import azure.functions as func
from azure.identity import DefaultAzureCredential
from azure.storage.blob import BlobClient, BlobServiceClient, ContainerClient

app = func.FunctionApp()

def upload_blob_data(blob_service_client: BlobServiceClient, container_name: str):

     time_now  = datetime.datetime.now().strftime('%m_%d_%Y_%H_%M_%S') 
     blob_client = blob_service_client.get_blob_client(container=container_name, blob=f"{time_now}.txt")
     data = b"Sample data for blob"

     # Upload the blob data - default blob type is BlockBlob    
     blob_client.upload_blob(data, blob_type="BlockBlob")

@app.timer_trigger(schedule="0 * * * * *", arg_name="myTimer", run_on_startup=False, use_monitor=False) 
def timer_trigger(myTimer: func.TimerRequest) -> None:

     account_url = "https://<storage>.blob.core.windows.net"
     credential = DefaultAzureCredential()

    # Create the BlobServiceClient object
     blob_service_client = BlobServiceClient(account_url, credential=credential)
     upload_blob_data(blob_service_client, "json")
     logging.info('Python timer trigger function executed.')

local.settings.json:

{
  "IsEncrypted": false,
  "Values": {
    "FUNCTIONS_WORKER_RUNTIME": "python",
    "AzureWebJobsFeatureFlags": "EnableWorkerIndexing",
    "AzureWebJobsStorage": "UseDevelopmentStorage=true"
  }
}

部署日志:

enter image description here

传送门:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.