我正在使用AWS SDK编写Powershell脚本,以便在S3中从存储桶中的特定目录下载指定数量的文件。
当我运行脚本时,我会在循环的每次迭代中得到此错误:
Read-S3Object : Illegal characters in path.
At line:21 char:5
+ Read-S3Object -BucketName $BucketName -Key $Key -File $LocalFile
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (Amazon.PowerShe...dS3ObjectCmdlet:ReadS3ObjectCmdlet) [Read-S3Object], Inv
alidOperationException
+ FullyQualifiedErrorId : Amazon.Runtime.AmazonServiceException,Amazon.PowerShell.Cmdlets.S3.ReadS3ObjectCmdlet
我尝试了一些变体,我怀疑这是包含以下内容的文件夹的问题:或\
Param(
[Parameter(Mandatory=$False)] [String]$WorkingDir = "C:\Temp\Testing\",
[Parameter(Mandatory=$False)] [String]$BucketName = "BucketName",
[Parameter(Mandatory=$False)] [String]$DownloadFolder = "Testing",
[Parameter(Mandatory=$False)] [int]$FileCount = 100
)
$FilesToDownload = Get-S3Object -BucketName $BucketName -KeyPrefix $DownloadFolder -MaxKey $FileCount
$FilesToDownload | ForEach-Object {
$Key = ($_.Key | Out-String)
$File = $Key.TrimStart($DownloadFolder + "/")
$LocalFile = Join-Path $WorkingDir $File
Read-S3Object -BucketName $BucketName -Key $Key -File $LocalFile
}
我可以让下面的内容工作,这正是ForEach循环正在做的事情:
Read-S3Object -BucketName BucketName -Key Testing/text.txt -File C:\Temp\Testing\test.txt
答案 0 :(得分:0)
看起来$ LocalFile变量中有一些非法字符,特别是回车,换行。
我通过这样做来修复它:
$LocalFile = $LocalFile -replace "`n",""
$LocalFile = $LocalFile -replace "`r",""
答案 1 :(得分:0)
真正的问题是以下几行。
import {ObjectId} from 'promised-mongo';
如果您只设置$ Key = $ _。键,那么您不会从out-string获得回车。
答案 2 :(得分:0)
这可能更容易做到:
Get-S3Object -BucketName ${BucketName} -Region ${RegionName} | Select-Object -First ${FileCount} | Copy-S3Object -LocalFolder ${WorkingDir}