繁体   English   中英

如何将在 Azure 函数应用程序中作为对 powershell Web 请求的响应而收到的文件保存到 Azure 存储容器?

[英]How to save the file received as response to powershell web request within an Azure function app to an Azure storage container?

我有一个 Azure 函数应用程序,它发出以下 Web 请求:

Invoke-RestMethod -Uri $uri -Method 'GET' -Headers $headers -Body $body -SkipCertificateCheck

在我的本地机器上,我可以将返回的XML文件作为OutFile保存到本地磁盘。 但是,在实际环境中,这是在 Azure 函数应用程序上,因此我认为我无法将文件保存到磁盘。 相反,我想将其重定向到 Azure 中的存储容器。

我尝试了 Azure 函数输出绑定以重定向那里的响应,但未能写入实际文件:

Push-OutputBinding -Name outputBlob -Value $response.content

所有这些对存储容器的写入都是一个字符串值。 那么如何将在 Azure 函数应用中作为对InvokeRestMethod响应接收到的实际文件写入此环境中的 Azure 存储容器?

除非文件很大,否则您确实可以将其保存到本地文件系统,即使在 Azure 上运行时也是如此。 只需选择正确的位置,不要忘记清理:

$f = New-TemporaryFile
try {
    Invoke-RestMethod ... -OutFile $f.FullName
    # Do whatever you want with this file
    ...
} finally {
    Remove-Item $f.FullName
}

请将您的文件读入字节数组并将其作为参数传递。

using namespace System.Net

# Input bindings are passed in via param block.
param($Request, $TriggerMetadata)

# Write to the Azure Functions log stream.
Write-Host "PowerShell HTTP trigger function processed a request."

New-Item -Path 'test.txt' -ItemType File
Set-Content 'test.txt' 'Welcome to www.thecodemanual.pl'
$file = [System.IO.File]::ReadAllBytes('test.txt')

Push-OutputBinding -Name outputBlob -Value $file

# Associate values to output bindings by calling 'Push-OutputBinding'.
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
    StatusCode = [HttpStatusCode]::OK
    Body = $body
})

然后你会得到:

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM