从包文件运行 Azure FunctionsRun your Azure Functions from a package file

在 Azure 中,可以直接从函数应用中的部署包文件运行函数。In Azure, you can run your functions directly from a deployment package file in your function app. 另一种做法是在函数应用的 d:\home\site\wwwroot 目录中部署文件。The other option is to deploy your files in the d:\home\site\wwwroot directory of your function app.

本文介绍从包运行函数的好处。This article describes the benefits of running your functions from a package. 此外,介绍如何在函数应用中启用此功能。It also shows how to enable this functionality in your function app.

从包文件运行的好处Benefits of running from a package file

从包文件运行函数可提供多种好处:There are several benefits to running from a package file:

  • 减少文件副本锁定问题的风险。Reduces the risk of file copy locking issues.
  • 可部署到生产应用(需重启)。Can be deployed to a production app (with restart).
  • 可以确定哪些文件在应用中运行。You can be certain of the files that are running in your app.
  • 提高 Azure 资源管理器部署的性能。Improves the performance of Azure Resource Manager deployments.
  • 可以减少冷启动时间,特别适用于具有大型 npm 包树的 JavaScript 函数。May reduce cold-start times, particularly for JavaScript functions with large npm package trees.

有关详细信息,请参阅此公告For more information, see this announcement.

使函数从包运行Enabling functions to run from a package

若要使函数应用从包运行,只需将 WEBSITE_RUN_FROM_PACKAGE 设置添加到函数应用设置。To enable your function app to run from a package, you just add a WEBSITE_RUN_FROM_PACKAGE setting to your function app settings. WEBSITE_RUN_FROM_PACKAGE 设置可以使用以下值之一:The WEBSITE_RUN_FROM_PACKAGE setting can have one of the following values:

ValueValue 说明Description
1 建议用于在 Windows 上运行的函数应用。Recommended for function apps running on Windows. 从函数应用的 d:\home\data\SitePackages 文件夹中的某个包文件运行。Run from a package file in the d:\home\data\SitePackages folder of your function app. 如果不使用 zip deploy 进行部署,则此选项要求该文件夹同时包含名为 packagename.txt 的文件。If not deploying with zip deploy, this option requires the folder to also have a file named packagename.txt. 此文件仅包含文件夹中包文件的名称(没有任何空白字符)。This file contains only the name of the package file in folder, without any whitespace.
<URL> 要运行的特定包文件的位置。Location of a specific package file you want to run. 使用 Blob 存储时,应通过共享访问签名 (SAS) 使用专用容器,使 Functions 运行时能够访问包。When using Blob storage, you should use a private container with a Shared Access Signature (SAS) to enable the Functions runtime to access to the package. 可以使用 Azure 存储资源管理器将包文件上传到 Blob 存储帐户。You can use the Azure Storage Explorer to upload package files to your Blob storage account. 指定 URL 时,还必须在发布更新的包后同步触发器When you specify a URL, you must also sync triggers after you publish an updated package.

Caution

在 Windows 上运行函数应用时,外部 URL 选项会导致糟糕的冷启动性能。When running a function app on Windows, the external URL option yields worse cold-start performance. 将函数应用部署到 Windows 时,应将 WEBSITE_RUN_FROM_PACKAGE 设置为 1 并通过 zip 部署进行发布。When deploying your function app to Windows, you should set WEBSITE_RUN_FROM_PACKAGE to 1 and publish with zip deployment.

下面显示了配置为从 Azure Blob 存储中托管的 .zip 文件运行的函数应用:The following shows a function app configured to run from a .zip file hosted in Azure Blob storage:

WEBSITE_RUN_FROM_ZIP 应用设置

Note

目前仅支持 .zip 包文件。Currently, only .zip package files are supported.

与 zip 部署集成Integration with zip deployment

Zip 部署是 Azure 应用服务的一项功能,可用于将函数应用项目部署到 wwwroot 目录。Zip deployment is a feature of Azure App Service that lets you deploy your function app project to the wwwroot directory. 项目打包为 .zip 部署文件。The project is packaged as a .zip deployment file. 可以使用相同的 API 将包部署到 d:\home\data\SitePackages 文件夹。The same APIs can be used to deploy your package to the d:\home\data\SitePackages folder. WEBSITE_RUN_FROM_PACKAGE 应用设置使用值 1 时,zip 部署 API 会将包复制到 d:\home\data\SitePackages 文件夹,而不是将文件提取到 d:\home\site\wwwrootWith the WEBSITE_RUN_FROM_PACKAGE app setting value of 1, the zip deployment APIs copy your package to the d:\home\data\SitePackages folder instead of extracting the files to d:\home\site\wwwroot. 它还会创建 packagename.txt 文件。It also creates the packagename.txt file. 重启后,该包将作为只读文件系统装载到 wwwrootAfter a restart, the package is mounted to wwwroot as a read-only filesystem. 有关 zip 部署的详细信息,请参阅 Azure Functions 的 Zip 部署For more information about zip deployment, see Zip deployment for Azure Functions.

添加 WEBSITE_RUN_FROM_PACKAGE 设置Adding the WEBSITE_RUN_FROM_PACKAGE setting

可以通过以下几种方法添加、更新和删除函数应用设置:There are several ways that you can add, update, and delete function app settings:

故障排除Troubleshooting

  • Run From Package 将 wwwroot 设为只读,因此在将文件写入此目录时将收到一个错误。Run From Package makes wwwroot read-only, so you will receive an error when writing files to this directory.
  • 不支持 Tar 和 gzip 格式。Tar and gzip formats are not supported.
  • 此功能不与本地缓存组合。This feature does not compose with local cache.
  • 若要提高冷启动性能,请使用本地 Zip 选项 (WEBSITE_RUN_FROM_PACKAGE=1)。For improved cold-start performance, use the local Zip option (WEBSITE_RUN_FROM_PACKAGE=1).