【问题标题】:How to write to a File in Powershell Core Azure Function如何在 Powershell Core Azure 函数中写入文件
【发布时间】:2019-09-06 16:29:32
【问题描述】:

是否可以在 Powershell Core Azure Function 中创建和写入本地文件?

这是我正在运行的脚本的摘录(使用 blob 存储触发器)。

param([byte[]] $dockerFile, $TriggerMetadata)

Write-Host "Processing updated DockerFile: $($TriggerMetadata.dockerFile)"

Write-Host "---------------------------------------------------------"
Write-Host "Create local Docker File using contents from blob storage"
New-Item -Name $TriggerMetadata.snapshotId -ItemType "file"
[io.file]::WriteAllBytes($TriggerMetadata.snapshotId, $dockerFile)
Get-Content $TriggerMetadata.snapshotId
Write-Host "---------------------------------------------------------"

使用New-Item 会抛出这个 -> ERROR: Could not find file 'D:\home\site\wwwroot\<filename>'.

而使用 [io.file]::WriteAllBytes 会抛出这个 -> Exception calling "WriteAllBytes" with "2" argument(s): "Could not find file 'D:\home\site\wwwroot\<filename>'."

Azure Functions 是否有权在本地创建文件?

【问题讨论】:

  • 您可以尝试获取临时文件夹并在那里写入吗? stackoverflow.com/a/34559554/1537195$parent = [System.IO.Path]::GetTempPath()
  • @silent 这确实有效! (我通过使用New-TemporaryFile 命令对其进行了一些更改,然后将FullName 传递给WriteAllBytes,但我认为这是相同答案的稍微优化的版本!如果你想把答案写下来,我'我会接受,否则我会放弃我的解决方案
  • 很高兴成功了!见下文

标签: powershell azure-functions serverless


【解决方案1】:

如上所述:使用New-TemporaryFile command 在临时位置创建一个新文件。然后你就可以给它写信了。

$TempFile = New-TemporaryFile
[io.file]::WriteAllBytes($TempFile.FullName, $dockerFile)

【讨论】:

    猜你喜欢
    • 2019-11-14
    • 1970-01-01
    • 2019-02-20
    • 1970-01-01
    • 2021-09-24
    • 1970-01-01
    • 1970-01-01
    • 2018-01-08
    • 2018-09-05
    相关资源
    最近更新 更多