8

我一直在尝试使用 AWS Update-LMFunctionCode 将我的文件部署到 AWS 中的现有 lambda 函数。

与我可以只提供 zipFile (-FunctionZip) 路径的 Publish-LMFunction 不同,Update-LMFunction 需要一个用于其 -Zipfile 参数的内存流。

有没有将本地 zipfile 从磁盘加载到有效的内存流中的示例?我最初的电话收到无法解压缩文件的错误...

$deployedFn =  Get-LMFunction -FunctionName $functionname
        "Function Exists - trying to update"
        try{
            [system.io.stream]$zipStream = [system.io.File]::OpenRead($zipFile)
        [byte[]]$filebytes = New-Object byte[] $zipStream.length
        [void] $zipStream.Read($filebytes, 0, $zipStream.Length)
            $zipStream.Close()
            "$($filebytes.length)"
        $zipString =  [System.Convert]::ToBase64String($filebytes)
        $ms = new-Object IO.MemoryStream
        $sw = new-Object IO.StreamWriter $ms
        $sw.Write($zipString)
        Update-LMFunctionCode -FunctionName $functionname -ZipFile $ms
            }
        catch{
             $ErrorMessage = $_.Exception.Message
            Write-Host $ErrorMessage
            break
        }

Powershell 函数的文档在这里:http ://docs.aws.amazon.com/powershell/latest/reference/items/Update-LMFunctionCode.html虽然它想生活在一个框架中......

4

1 回答 1

11

尝试使用该CopyTo方法从一个流复制到另一个:

try {
    $zipFilePath = "index.zip"
    $zipFileItem = Get-Item -Path $zipFilePath
    $fileStream = $zipFileItem.OpenRead()
    $memoryStream = New-Object System.IO.MemoryStream
    $fileStream.CopyTo($memoryStream)

    Update-LMFunctionCode -FunctionName "PSDeployed" -ZipFile $memoryStream
}
finally {
    $fileStream.Close()
}
于 2015-08-21T17:45:33.500 回答