Azure Function App 无法启动 Docker 容器,原因是“Azure.RequestFailedException:指定的资源名称包含无效字符。”

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

我们有一个从 Docker 映像部署的 Azure Function 应用程序。

该函数正在运行 .NET 8.0,FUNCTIONS_EXTENSION_VERSION = ~4 且 FUNCTIONS_WORKER_RUNTIME = dotnet-isolated。

该应用程序包含一个非常简单的 BlobTrigger 函数,用于记录找到的 blob 的名称。

[Function("BlobStorageFunction")]
public async Task Run([BlobTrigger("sftp/upload/{name}.{extension}", Connection = "AzureWebJobsStorage")] byte\[] inputBlob, string name, string extension, CancellationToken cancellationToken)
{
...
}

该镜像将在我们的开发功能应用程序中成功运行,没有任何问题。

生产环境升级到最新镜像时,该功能无法启动。查看日志,当函数初始化时,抛出的唯一错误如下:

2024-01-04T12:47:49.460768296Z MS_FUNCTION_AZURE_MONITOR_EVENT 4,productiongroup.azurewebsites.net,Microsoft.Web/sites/functions/log,FunctionAppLogs,uksouth,"{'appName':'productiongroup','roleInstance':'f7a339e552b408f55b8e6e319dc836bb00141ae75da7404e698db60116fd5efd','message':'Request [74ff0795-01e7-44a0-98a4-e8a5b76d4547] GET https://productionstorage.blob.core.windows.net/$logs?restype=container&comp=list&prefix=blob%2F2024%2F01%2F04%2F1200&include=Metadata\r\nx-ms-version:2022-11-02\nAccept:application/xml\nx-ms-client-request-id:74ff0795-01e7-44a0-98a4-e8a5b76d4547\nx-ms-return-client-request-id:true\nUser-Agent:azsdk-net-Storage.Blobs/12.16.0 (.NET 6.0.25; Linux 5.15.131.1-2.cm2 #1 SMP Sun Sep 24 03:38:45 UTC 2023)\nx-ms-date:Thu, 04 Jan 2024 12:47:49 GMT\nAuthorization:REDACTED\nclient assembly: Azure.Storage.Blobs','category':'Azure.Core.1','hostVersion':'4.27.7.7','hostInstanceId':'004b1b1f-2bf0-4047-b5a8-3db7242f96c2','level':'Information','levelId':2,'processId':1,'eventId':1,'eventName':'Request'}",01/04/2024 12:47:49
2024-01-04T12:47:49.481834310Z [41m[30mfail[39m[22m[49m: Microsoft.Azure.WebJobs.Script.WebHost.WebScriptHostExceptionHandler[0]
2024-01-04T12:47:49.481871911Z       An unhandled exception has occurred. Host is shutting down.
2024-01-04T12:47:49.481880011Z       Azure.RequestFailedException: The specifed resource name contains invalid characters.
2024-01-04T12:47:49.481886811Z       RequestId:dca7a63e-1003-0064-210c-3f1f5c000000
2024-01-04T12:47:49.481890711Z       Time:2024-01-04T12:47:49.4544438Z
2024-01-04T12:47:49.481894611Z       Status: 400 (The specifed resource name contains invalid characters.)
2024-01-04T12:47:49.481898211Z       ErrorCode: InvalidResourceName
2024-01-04T12:47:49.481901511Z       
2024-01-04T12:47:49.481904611Z       Content:
2024-01-04T12:47:49.481907811Z       <?xml version="1.0" encoding="utf-8"?><Error><Code>InvalidResourceName</Code><Message>The specifed resource name contains invalid characters.
2024-01-04T12:47:49.481912811Z       RequestId:dca7a63e-1003-0064-210c-3f1f5c000000
2024-01-04T12:47:49.481916211Z       Time:2024-01-04T12:47:49.4544438Z</Message></Error>
2024-01-04T12:47:49.481920311Z       
2024-01-04T12:47:49.481925111Z       Headers:
2024-01-04T12:47:49.481930412Z       Server: Windows-Azure-Queue/1.0 Microsoft-HTTPAPI/2.0
2024-01-04T12:47:49.481936112Z       x-ms-request-id: dca7a63e-1003-0064-210c-3f1f5c000000
2024-01-04T12:47:49.481942112Z       x-ms-version: 2018-11-09
2024-01-04T12:47:49.481946512Z       x-ms-error-code: InvalidResourceName
2024-01-04T12:47:49.481949812Z       Date: Thu, 04 Jan 2024 12:47:48 GMT
2024-01-04T12:47:49.481953112Z       Content-Length: 243
2024-01-04T12:47:49.481956412Z       Content-Type: application/xml
2024-01-04T12:47:49.481959712Z       
2024-01-04T12:47:49.481964412Z          at Azure.Storage.Queues.QueueRestClient.GetPropertiesAsync(Nullable`1 timeout, CancellationToken cancellationToken)
2024-01-04T12:47:49.481980912Z          at Azure.Storage.Queues.QueueClient.GetPropertiesInternal(Boolean async, CancellationToken cancellationToken, String operationName)
2024-01-04T12:47:49.481986012Z          at Azure.Storage.Queues.QueueClient.ExistsInternal(Boolean async, CancellationToken cancellationToken)
2024-01-04T12:47:49.481989412Z          at Azure.Storage.Queues.QueueClient.ExistsAsync(CancellationToken cancellationToken)
2024-01-04T12:47:49.481992712Z          at Microsoft.Azure.WebJobs.Extensions.Storage.Common.Listeners.QueueListener.ExecuteAsync(CancellationToken cancellationToken)
2024-01-04T12:47:49.481996313Z          at Microsoft.Azure.WebJobs.Extensions.Storage.Common.Timers.TaskSeriesTimer.RunAsync(CancellationToken cancellationToken)

我们比较了 Azure 中两个应用程序服务的配置设置,但没有发现任何差异。

如前所述,该函数在本地运行,通过docker在本地运行,在另一个环境中运行。

我们是否遗漏了一些明显的东西?

azure azure-functions azure-blob-storage
1个回答
0
投票

我创建了一个 Blob 触发器函数并使用 Docker Image 部署到函数应用程序。我已按照以下步骤操作-

  1. 使用以下命令创建了 Blob 触发器功能。
func init --worker-runtime dotnet-isolated --docker
func new --name BlobTriggeredFunction --template "Blob trigger"
[Function(nameof(BlobTriggeredFunction))]
public async Task Run([BlobTrigger("demo-container/{name}", Connection = "AzureWebJobsStorage")] Stream stream, string name)
{
    using var blobStreamReader = new StreamReader(stream);
    var content = await blobStreamReader.ReadToEndAsync();
    _logger.LogInformation($"C# Blob trigger function Processed blob\n Name: {name} \n Data: {content}");
}
  1. 修改了Dockerfile如下。
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS installer-env

COPY . /src/dotnet-function-app
RUN cd /src/dotnet-function-app && \
mkdir -p /home/site/wwwroot && \
dotnet publish *.csproj --output /home/site/wwwroot

ENV AzureWebJobsStorage="Connection_string"

FROM mcr.microsoft.com/azure-functions/dotnet-isolated:4.0-dotnet-isolated8.0
ENV AzureWebJobsScriptRoot=/home/site/wwwroot \
    AzureFunctionsJobHost__Logging__Console__IsEnabled=true

COPY --from=installer-env ["/home/site/wwwroot", "/home/site/wwwroot"]

ENV AzureWebJobsStorage="Connection_string"
  1. 我已按照此 MS Docs 在 Docker 映像中本地构建和运行我的函数。
docker build --tag <DOCKER_ID>/azurefunctionsimage:v1.0.0 .
docker run -p 8080:80 -it <DOCKER_ID>/azurefunctionsimage:v1.0.0
  1. 我将函数代码推送到 Azure 容器注册表。
az acr login --name <REGISTRY_NAME>
docker tag <DOCKER_ID>/azurefunctionsimage:v1.0.0 <LOGIN_SERVER>/azurefunctionsimage:v1.0.0
docker push <LOGIN_SERVER>/azurefunctionsimage:v1.0.0
az acr update -n <REGISTRY_NAME> --admin-enabled true
  1. 我使用 Azure 容器注册表凭据将函数发布到 Function App。
az functionapp create --name <Function_APP_NAME> --storage-account <STORAGE_NAME> --resource-group <Resource_group> --plan <Plan_Name> --functions-version 4 --runtime dotnet-isolated --image <LOGIN_SERVER>/azurefunctionsimage:v1.0.0 --registry-username <USERNAME> --registry-password <SECURE_PASSWORD>

我也可以在函数应用程序和容器详细信息中看到我的函数。

enter image description here

enter image description here

在存储帐户中上传文件后,功能被触发。

enter image description here

按照提到的步骤操作,您也会得到结果,因为我还使用带有最新图像的.NET 8隔离函数获得输出。

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