在Powershell中,我如何获得本地zip文件的Base64encoded内存流?

Jef*_*tin 8 powershell amazon-web-services aws-powershell aws-lambda

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

与Publish-LMFunction不同,我只能提供zipFile(-FunctionZip)的路径,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
        }
Run Code Online (Sandbox Code Playgroud)

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

Jam*_*mes 10

尝试使用该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()
}
Run Code Online (Sandbox Code Playgroud)

  • 我认为该文本是从底层[Lambda API Reference for UpdateFunctionCode](http://docs.aws.amazon.com/lambda/latest/dg/API_UpdateFunctionCode.html)复制的.当然,这并不能解释为什么他们的PowerShell cmdlet不能只采用文件路径并弄清楚其余的:). (3认同)