简体   繁体   English

从S3存储桶+文件夹下载文件

[英]Download files from S3 bucket + folder

I am writing a Powershell script using the AWS SDK to download a specified amount of files from a particular directory, within a bucket, in S3. 我正在使用AWS开发工具包编写Powershell脚本,以从S3中存储桶中的特定目录中下载指定数量的文件。

When I run the script, I get this error, for each iteration of the loop: 运行脚本时,对于循环的每次迭代,都会收到此错误:

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

I have tried a few variations, I suspected it was an issue with the folder containing : or \\ 我尝试了一些变化,我怀疑这是包含:或\\的文件夹的问题

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

    }

I can get the below to work, which is essentially what the ForEach loop is doing: 我可以使以下内容起作用,这基本上就是ForEach循环正在做的事情:

Read-S3Object -BucketName BucketName -Key Testing/text.txt -File C:\Temp\Testing\test.txt

Looks like there was some illegal characters in the $LocalFile variable, particularly carriage return, line-feed. 看起来$ LocalFile变量中有一些非法字符,尤其是回车符,换行符。

I fixed it by doing: 我通过以下方法解决了它:

$LocalFile = $LocalFile -replace "`n",""
$LocalFile = $LocalFile -replace "`r",""

The real problem is the following line. 真正的问题是以下几行。

$Key       = ($_.Key | Out-String)

If you just set $Key = $_.Key then you don't get the carriage return from out-string. 如果只设置$ Key = $ _。Key,则不会从字符串中得到回车符。

这可能更简单:

Get-S3Object -BucketName ${BucketName} -Region ${RegionName} | Select-Object -First ${FileCount} | Copy-S3Object -LocalFolder ${WorkingDir}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM