【问题标题】:Azure Functions Temp storageAzure Functions 临时存储
【发布时间】:2018-02-27 21:43:57
【问题描述】:

当我尝试将文件保存到 Azure Functions 目录 (D:\home\data\temp\response.pdf) 中的临时存储时,我收到以下错误。为什么我不能写入这个目录?

mscorlib: Exception has been thrown by the target of an invocation. System: An exception occurred during a WebClient request. mscorlib: ***Could not find a part of the path 'D:\home\data\temp\response.pdf'.***
2017-09-19T07:05:24.353 Function completed (Failure, Id=3aa4b740-ba8a-465c-ad7c-75b38fa2a472, Duration=334ms)
2017-09-19T07:06:31  No new trace in the past 1 min(s).

【问题讨论】:

  • 当我在本地系统上尝试相同的代码时,它工作正常。
  • 请确保D:\home\data\temp 存在。我们可以使用 Kudu 工具(youfunctionappName.scm.azurewebsites.net) 来检查。
  • D:\home\data\temp 文件夹确实存在。从控制台创建的
  • 你也可以写到:D:\local\Temp,这个Temp目录对我来说已经存在了。

标签: azure azure-web-app-service azure-functions


【解决方案1】:

我建议使用System.IO.Path.GetTempPath(),因为这将始终为我们提供任何给定系统的有效路径。

此外,对于给定的实例,函数可能会同时执行多次,因此最好确保每次执行都有唯一的路径。这是一个简单的例子:

var tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());

或者,我们可以使用System.IO.Path.GetTempFileName(),它会在返回完整路径和唯一文件名之前额外创建文件。

【讨论】:

  • 对于那些支持的人,不要忘记也支持这个问题:)
【解决方案2】:

我找到了更好的选择。您可以使用System.IO.Path.GetTempFileName() 创建一个%userprofile%\Local\Temp\tmpE128.tmp 文件

【讨论】:

  • 我分享了一个更安全的方法来获取临时文件名System.IO.Path.Combine(System.IO.Path.GetTempPath(), System.IO.Path.GetRandomFileName())
【解决方案3】:

根据异常,您的函数项目中似乎不存在D:\home\data\temp\。请尝试使用 Azure Kudu 工具 (https://yourwebsiteName.scm.azurewebsites.net) 进行检查。如果路径不存在,请尝试添加临时文件夹并重试。

我们可以从Azure Web App sandbox 获得有关 Azure WebApp 的更多信息。有关 azure 文件结构的更多详细信息,请参阅document

【讨论】:

  • D:\home\data\temp 文件夹确实存在。从控制台创建。
  • 我创建了一个 httptrigger 函数并在我这边测试它,它工作正常。我的测试代码:File.WriteAllText(@"D:\home\data\temp\test.txt","tomtest");
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-29
  • 2021-01-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多