无法在 Azure Functions 中创建文件
Unable to create file in Azure Functions
我在应用服务计划中有一个 Azure 函数 运行。此函数应用程序从 sftp 下载文件并执行一些操作 process/validation,然后上传到 blob 存储。
这是工作代码,我必须进行一些更改,以便我必须保存物理文件 FileStream 而不是 MemoryStream。这在我的本地环境中也有效,但一旦部署到 Azure,我在下载 sftp 文件的步骤中收到 FileNotFound 错误。
错误文本(已编辑 file/class 个名称)
System.IO.FileNotFoundException: Could not find file 'D:\home\site\wwwroot\myfile.csv'.
File name: 'D:\home\site\wwwroot\myfile.csv'
at System.IO.FileStream.ValidateFileHandle(SafeFileHandle fileHandle)
at System.IO.FileStream.CreateFileOpenHandle(FileMode mode, FileShare share, FileOptions options)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options)
at MyProcessDataLoadProcess.DownLoadMyProcessFilesFromSftp.DownloadFileFromSftp(SftpClient sftp, SftpFile sftpFile, String localRootPath) in D:\a\s\MyProcessDataLoadProcess\DownLoadMyProcessFilesFromSftp.cs:line 173
at MyProcessDataLoadProcess.DownLoadMyProcessFilesFromSftp.Run(TimerInfo timerInfo, ILogger log, ExecutionContext context) in D:\a\s\MyProcessDataLoadProcess\DownLoadMyProcessFilesFromSftp.cs:line 106
我确定这与权限有关,因为当我尝试使用平台功能命令中的简单目录 . > test.txt 创建测试文件时线工具,我得到 "system could not find file specefied error"。
我确信这与允许写入本地磁盘的设置有关,因为我使用不同的订阅进行了 POC,我拥有完全权限。我不确定这是否可以在应用程序设置中的某处进行配置,或者是否需要在存储级别或订阅级别授予权限。
更新:
目前,我找到了使用 Path.GetTempPath() 来使用临时文件夹的解决方法。但是,我有点困惑是什么限制阻止我在根文件夹中创建文件。
如果 WEBSITE_RUN_FROM_PACKAGE
应用程序设置值为 1,zip 部署 API 会将您的包复制到 d:\home\data\SitePackages 文件夹,而不是将文件解压缩到 d:\home\site\wwwroot。它还会创建 packagename.txt 文件。重启后,该包作为只读文件系统挂载到 wwwroot。 运行 From Package 使 wwwroot 成为只读的,因此您在向该目录写入文件时会收到错误消息。
嗯,就我而言,它不允许我更改 WEBSITE_RUN_FROM_PACKAGE 值。对我有用的是只需将部署方法从 Zip 部署更改为 Web 部署。实际上,当我们进行 Zip 部署时,我们的文件夹结构在云中变为只读,因此不允许在运行时创建文件夹和文件,我们会收到上述错误。 Web 部署解决了这个问题,让我们可以在运行时自由地在云中使用我们的解决方案资源管理器。
详情请参考下方link:
https://tomasherceg.com/blog/post/azure-app-service-cannot-create-directories-and-write-to-filesystem-when-deployed-using-azure-devops
我在应用服务计划中有一个 Azure 函数 运行。此函数应用程序从 sftp 下载文件并执行一些操作 process/validation,然后上传到 blob 存储。
这是工作代码,我必须进行一些更改,以便我必须保存物理文件 FileStream 而不是 MemoryStream。这在我的本地环境中也有效,但一旦部署到 Azure,我在下载 sftp 文件的步骤中收到 FileNotFound 错误。
错误文本(已编辑 file/class 个名称)
System.IO.FileNotFoundException: Could not find file 'D:\home\site\wwwroot\myfile.csv'.
File name: 'D:\home\site\wwwroot\myfile.csv'
at System.IO.FileStream.ValidateFileHandle(SafeFileHandle fileHandle)
at System.IO.FileStream.CreateFileOpenHandle(FileMode mode, FileShare share, FileOptions options)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options)
at MyProcessDataLoadProcess.DownLoadMyProcessFilesFromSftp.DownloadFileFromSftp(SftpClient sftp, SftpFile sftpFile, String localRootPath) in D:\a\s\MyProcessDataLoadProcess\DownLoadMyProcessFilesFromSftp.cs:line 173
at MyProcessDataLoadProcess.DownLoadMyProcessFilesFromSftp.Run(TimerInfo timerInfo, ILogger log, ExecutionContext context) in D:\a\s\MyProcessDataLoadProcess\DownLoadMyProcessFilesFromSftp.cs:line 106
我确定这与权限有关,因为当我尝试使用平台功能命令中的简单目录 . > test.txt 创建测试文件时线工具,我得到 "system could not find file specefied error"。
我确信这与允许写入本地磁盘的设置有关,因为我使用不同的订阅进行了 POC,我拥有完全权限。我不确定这是否可以在应用程序设置中的某处进行配置,或者是否需要在存储级别或订阅级别授予权限。
更新:
目前,我找到了使用 Path.GetTempPath() 来使用临时文件夹的解决方法。但是,我有点困惑是什么限制阻止我在根文件夹中创建文件。
如果 WEBSITE_RUN_FROM_PACKAGE
应用程序设置值为 1,zip 部署 API 会将您的包复制到 d:\home\data\SitePackages 文件夹,而不是将文件解压缩到 d:\home\site\wwwroot。它还会创建 packagename.txt 文件。重启后,该包作为只读文件系统挂载到 wwwroot。 运行 From Package 使 wwwroot 成为只读的,因此您在向该目录写入文件时会收到错误消息。
嗯,就我而言,它不允许我更改 WEBSITE_RUN_FROM_PACKAGE 值。对我有用的是只需将部署方法从 Zip 部署更改为 Web 部署。实际上,当我们进行 Zip 部署时,我们的文件夹结构在云中变为只读,因此不允许在运行时创建文件夹和文件,我们会收到上述错误。 Web 部署解决了这个问题,让我们可以在运行时自由地在云中使用我们的解决方案资源管理器。 详情请参考下方link: https://tomasherceg.com/blog/post/azure-app-service-cannot-create-directories-and-write-to-filesystem-when-deployed-using-azure-devops