In Powershell, how can I get a Base64encoded storage file of a local zip file?

I am trying to use AWS Update-LMFunctionCode to deploy my file with an existing lambda function in AWS.

Unlike the Publish-LMFunction function, where I can only provide the path to the zipFile (-FunctionZip), Update-LMFunction wants to have memystream for its -Zipfile argument.

is there an example of loading a local zip file from disk into a memory stream that works? My initial calls get errors that the file cannot be unzipped ...

$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 } 

The docs for the Powershell function are here: http://docs.aws.amazon.com/powershell/latest/reference/items/Update-LMFunctionCode.html , although it wants to live in a frame ...

+7
powershell amazon-web-services aws-lambda aws-powershell
source share
1 answer

Try using the CopyTo method to copy from one stream to another:

 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() } 
+8
source share

All Articles