![](/img/trans.png)
[英]Copying new files from newly created folders in my S3 bucket to my local machine using PowerShell
[英]PowerShell for AWS: List only "folders" from S3 bucket?
有沒有什么簡單的方法可以使用 PowerShell 只從 S3 存儲桶中獲取“文件夾”列表,而不列出每個對象,而只是編寫不同路徑的編譯列表? 我正在處理的存儲桶中有數十萬個單獨的對象,這需要很長時間。
這可能是一個非常愚蠢的問題,如果是這種情況,我很抱歉,但我在 Google 或 SO 上找不到任何內容來回答這個問題。 我已經嘗試向 Get-S3Object 的 -KeyPrefix 和 -Key 參數添加通配符,但無濟於事。 這是唯一一個似乎能夠完成我所追求的 cmdlet 的 cmdlet。
毫無意義的背景故事:我只想確保將文件傳輸到正確的現有文件夾。 我是簽約第三方,所以我沒有控制台登錄訪問權限,也不是維護 AWS 賬戶的人。
我知道這可以使用 Java 和 C# 等實現,但我正在做與這個相當簡單的 PS 項目相關的所有其他工作,並希望能夠堅持下去。
提前致謝。
您可以使用AWS Tools For PowerShell列出存儲桶中的對象(通過Get-S3Object )並從響應對象中提取通用前綴。
下面是一個遞歸檢索子目錄的小庫:
function Get-Subdirectories
{
param
(
[string] $BucketName,
[string] $KeyPrefix,
[bool] $Recurse
)
@(get-s3object -BucketName $BucketName -KeyPrefix $KeyPrefix -Delimiter '/') | Out-Null
if($AWSHistory.LastCommand.Responses.Last.CommonPrefixes.Count -eq 0)
{
return
}
$AWSHistory.LastCommand.Responses.Last.CommonPrefixes
if($Recurse)
{
$AWSHistory.LastCommand.Responses.Last.CommonPrefixes | % { Get-Subdirectories -BucketName $BucketName -KeyPrefix $_ -Recurse $Recurse }
}
}
function Get-S3Directories
{
param
(
[string] $BucketName,
[bool] $Recurse = $false
)
Get-Subdirectories -BucketName $BucketName -KeyPrefix '/' -Recurse $Recurse
}
此遞歸函數依賴於在每次迭代時更新 KeyPrefix,以檢查傳遞給它的每個 KeyPrefix 中的子目錄。 通過將分隔符設置為'/'
,在命中分隔符的第一次出現之前匹配 KeyPrefix 字符串的鍵將被滾動到 $AWSHistory 的最后一個響應中的 CommonPrefixes 集合中。
要僅檢索 S3 存儲桶中的頂級目錄:
PS C:/> Get-S3Directories -BucketName 'myBucket'
要檢索 S3 存儲桶中的所有目錄:
PS C:/> Get-S3Directories -BucketName 'myBucket' -Recurse $true
這將返回一個字符串集合,其中每個字符串都是一個公共前綴。
示例輸出:
myprefix/
myprefix/txt/
myprefix/img/
myotherprefix/
...
$objects = Get-S3Object -BucketName $bucketname -ProfileName $profilename -Region $region
$paths=@()
foreach($object in $objects)
{
$path = split-path $object.Key -Parent
$paths += $path
}
$paths = $paths | select -Unique
write-host "`nNumber of folders "$paths.count""
Write-host "$([string]::join("`n",$paths)) "
此版本的 Powershell 在單個 S3 Bucket 中迭代超過 1000 個鍵(aws 僅限制 API get-S3object 的 1000 個鍵,因此我們需要一個 while 循環來獲取超過 1000 個鍵(又名文件夾))在輸出生成到 csv 后,記得對重復項進行排序Excel 刪除重復項(PS,任何人都可以協助對重復項進行排序,因為我認為我的腳本不能很好地處理重復項)
#Main-Code
$keysPerPage = 1000 #Set max key of AWS limit of 1000
$bucketN = 'testBucket' #Bucketname
$nextMarker = $null
$output =@()
$Start = "S3 Bucket Name : $bucketN"
$End = "- End of Folder List -"
Do
{
#Iterate 1000 records per do-while loop, this is to overcome the limitation of only 1000 keys retrieval per get-s3object calls by AWS
$batch = get-s3object -BucketName $bucketN -Maxkey $keysPerPage -Marker $nextMarker
$batch2 = $batch.key | % {$_.Split('/')[0]} | Sort -Unique
$output += $batch2
$batch2
$nextMarker= $AWSHistory.LastServiceResponse.NextMarker
} while ($nextMarker)
#Output to specific folder in a directory
$Start | Out-file C:\Output-Result.csv -Append
$output | Out-file C:\Output-Result.csv -Append
$End | Out-file C:\Output-Result.csv -Append
接受的答案是正確的,但有缺陷。 如果您有一個包含許多“文件夾”(超過 1000 個)的大存儲桶,您將只能使用以下命令獲取最后 1000 個前綴:
$AWSHistory.LastCommand.Responses.Last.CommonPrefixes
AWS 以 1000 為增量對響應進行批處理。 如果你看
$AWSHistory.LastCommand.Responses.History
您將看到多個條目。 不幸的是,默認情況下只有 5 個。 您可以使用 Set-AWSHistoryConfiguration 函數更改該行為。
要增加歷史響應的數量,請使用 -MaxServiceCallHistory 參數。
Set-AWSHistoryConfiguration -MaxServiceCallHistory 20
這將存儲下一個(以及所有后續)命令的最后 20 個服務調用。
通過上述配置,您最多可以從一個文件夾中檢索 20000 個子文件夾。
要檢索所有文件夾,請執行以下操作:
$subFolders = ($AwsHistory.LastCommand.Responses.History).CommonPrefixes
注意:增加配置參數會占用更多內存。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.