首页 > 解决方案 > 为什么我的 powershell 上传到 lambda 将目录视为文件?

问题描述

使用以下脚本

$tmpdir = New-TemporaryFile | %{ rm $_; mkdir $_ }
$tempFile = New-TemporaryFile
$ziplocation = $tempFile.FullName + ".zip"
$ziplocation 
$filepath = Get-ChildItem $file
$filename = $filepath.Name
$fileWithoutExtension = $filepath.BaseName
$directory = Split-Path -path $file -parent
$sharedFolder = Join-Path -Path $directory -ChildPath "/shared"
$sharedFolder
$lambdaName = "$($enviroment)__$($fileWithoutExtension)"
$s3path = "$($enviroment)/$($lambdaName).zip"
$s3fullPath = "s3://firmware-repo-source-code/$($enviroment)/$($lambdaName).zip"

Copy-Item -Path $file -Destination $tmpdir
"Base file copied"
Rename-Item  -path(Join-Path -Path $tmpdir -ChildPath $filename) -NewName "index.js"

Copy-Item -Path $sharedFolder -Destination $tmpdir -Recurse -Container
"shared files copied"

#Compress-Archive -Path $tmpdir -DestinationPath $ziplocation
Compress-Archive -Path ($tmpdir.FullName + "\*") -DestinationPath $ziplocation
"zip created"

aws lambda update-function-code --function-name $lambdaName --zip-file ("fileb://" + $ziplocation)
"Lambda updated"

function New-TemporaryDirectory {
    $parent = [System.IO.Path]::GetTempPath()
    [string] $name = [System.Guid]::NewGuid()
    New-Item -ItemType Directory -Path (Join-Path $parent $name)
}

我最终的 lambda 项目结构如下所示: 在此处输入图像描述

我在 Windows 机器上运行 powershell 版本 5.1.17763.316。我可以以编程方式将 zip 上传到 s3 并在文件资源管理器中查看,但如果我通过 console.aws.com 上传相同的 zip,我也会遇到同样的问题。

标签: powershellaws-lambda

解决方案


ZIP 标准使用 / 作为路径分隔符,但 Powershell 中的 Compress-Archive 使用 \,所以当它被 Lambda 解压缩时,它认为它们是长文件名而不是分隔路径。


推荐阅读