如何使用PowerShell在本地备份S3 Bucket的内容

时间:2014-09-08 15:25:09

标签: powershell amazon-web-services amazon-s3

我创建了这个函数来从S3 Bucket中提取对象。它有效,但由于-Key参数,我一次只能做一个文件。

无论如何备份存储桶的全部内容而不编写多个Copy-S3Object cmdlet?

function CopyFromS3ToFolder($S3_Bucket, $S3_Folder_Destination, $S3_Key, $S3_SecretKey, $S3_AccessKey, $S3_Region)
{
    #http://docs.aws.amazon.com/powershell/latest/reference/Index.html (Amazon Simple Storage Service)
    #version AWSToolsAndSDKForNet_sdk-2.0.11.0-ps-2.0.11.0-tk-1.6.5.2    

    Write-Host "Copying from S3 to Local Directory"
    Write-Host "Folder Name :$S3_Folder_Destination"         
    Copy-S3Object -BucketName $S3_Bucket -LocalFile $S3_Folder_Destination -SecretKey $S3_SecretKey -AccessKey $S3_AccessKey -Region $S3_Region -Key $S3_Key
}

2 个答案:

答案 0 :(得分:2)

您可以使用Get-S3Object获取所需的密钥,然后将其传递给Copy-S3Object,如下所示:

Get-S3Object -bucketname "Bucket1" | %{ Copy-S3Object -Key $_.key -LocalFile "path" }

答案 1 :(得分:2)

$objects = Get-S3Object -bucketname $S3_Bucket  -SecretKey $S3_SecretKey -AccessKey $S3_AccessKey -Region $S3_Region -KeyPrefix 8.9.2014

foreach($key in $objects.key)
    {
        $filename = $key -replace "8.9.2014/"
        Copy-S3Object -Bucket $S3_Bucket -Key $key "$S3_Folder_Destination\$filename" -SecretKey $S3_SecretKey -AccessKey $S3_AccessKey -Region $S3_Region
    }

请参阅:https://forums.aws.amazon.com/thread.jspa?messageID=441291