从 S3 存储桶 + 文件夹下载文件
Download files from S3 bucket + folder
我正在使用 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
$LocalFile 变量中似乎有一些非法字符,尤其是回车符 return、换行符。
我通过以下方式修复了它:
$LocalFile = $LocalFile -replace "`n",""
$LocalFile = $LocalFile -replace "`r",""
真正的问题是下面一行。
$Key = ($_.Key | Out-String)
如果你只是设置 $Key = $_.Key 那么你不会从 out-string 中得到回车符 return。
这可能更简单:
Get-S3Object -BucketName ${BucketName} -Region ${RegionName} | Select-Object -First ${FileCount} | Copy-S3Object -LocalFolder ${WorkingDir}
我正在使用 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
$LocalFile 变量中似乎有一些非法字符,尤其是回车符 return、换行符。
我通过以下方式修复了它:
$LocalFile = $LocalFile -replace "`n",""
$LocalFile = $LocalFile -replace "`r",""
真正的问题是下面一行。
$Key = ($_.Key | Out-String)
如果你只是设置 $Key = $_.Key 那么你不会从 out-string 中得到回车符 return。
这可能更简单:
Get-S3Object -BucketName ${BucketName} -Region ${RegionName} | Select-Object -First ${FileCount} | Copy-S3Object -LocalFolder ${WorkingDir}