简体   繁体   English

使用 PowerShell 脚本将 Amazon S3 存储桶内容复制到本地文件夹

[英]Copy Amazon S3 Bucket Contents to Local Folder using PowerShell Script

I found a PowerShell script to copy all files from an Amazon S3 bucket to a local folder on one of our on-site servers:我找到了一个 PowerShell 脚本,可以将 Amazon S3 存储桶中的所有文件复制到我们现场服务器之一的本地文件夹中:

# Your account access key - must have read access to your S3 Bucket
$accessKey = "MyAccessKey"
# Your account secret access key
$secretKey = "MySecretKey"
# The region associated with your bucket e.g. eu-west-1, us-east-1 etc. (see http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/using-regions-availability-zones.html#concepts-regions)
$region = "eu-west-1"
# The name of your S3 Bucket
$bucket = "MyBucket/"
# The folder in your bucket to copy, including trailing slash. Leave blank to copy the entire bucket
$keyPrefix = ""
# The local file path where files should be copied
$localPath = "C:\test\"   

$objects = Get-S3Object -BucketName $bucket -KeyPrefix $keyPrefix -AccessKey $accessKey -SecretKey $secretKey -Region $region

foreach($object in $objects) {
$localFileName = $object.Key -replace $keyPrefix, ''
if ($localFileName -ne '') {
    $localFilePath = Join-Path $localPath $localFileName
    Copy-S3Object -BucketName $bucket -Key $object.Key -LocalFile $localFilePath -AccessKey $accessKey -SecretKey $secretKey -Region $region
}
}

If I run the first part on it's own (up to and including the Get-S3Object line) and then display the $objects variable it displays the details of the files one after the other with the below info in PowerShell如果我自己运行第一部分(直到并包括 Get-S3Object 行),然后显示$objects变量,它将一个接一个地显示文件的详细信息,并在 PowerShell 中提供以下信息

ETag : "38763873d83763c3876"
BucketName : MyBucketName
Key : 3287653_32876_to_38763_3876_client.xml
LastModified : 14/10/2016 11:26:51
Owner : Amazon.S3.Model.Owner
Size : 485
StorageClass : Standard

So it's successfully getting the files from the bucket... However, if I run the whole script I get an error for each loop iteration which says:所以它成功地从存储桶中获取文件......但是,如果我运行整个脚本,我会在每个循环迭代中得到一个错误,它说:

Copy-S3Object : The specified key does not exist.
At line:5 char:9
+         Copy-S3Object -BucketName $bucket -Key $object.Key -LocalFile ...
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidOperation: (Amazon.PowerShe...yS3ObjectCmdlet:CopyS3ObjectCmdlet) [Copy-S3Object]
, InvalidOperationException
+ FullyQualifiedErrorId : Amazon.S3.AmazonS3Exception,Amazon.PowerShell.Cmdlets.S3.CopyS3ObjectCmdlet

I therefore suspect it's something to do with the command or variables from the loop...因此,我怀疑这与循环中的命令或变量有关...

foreach($object in $objects) {
$localFileName = $object.Key -replace $keyPrefix, ''
if ($localFileName -ne '') {
    $localFilePath = Join-Path $localPath $localFileName
    Copy-S3Object -BucketName $bucket -Key $object.Key -LocalFile $localFilePath -AccessKey $accessKey -SecretKey $secretKey -Region $region
}
}

...but have not been able to figure out what. ......但一直无法弄清楚是什么。

Does anyone have any bright ideas on what I'm doing wrong please?有没有人对我做错了什么有什么好主意?

The problem is, that you are trying to use Copy-S3Object on something that is not an object.问题是,您试图在不是对象的东西上使用Copy-S3Object

What you perceive as 'folders' in S3 are really not folders, but just prefixes of the object name.您在 S3 中认为的“文件夹”实际上不是文件夹,而只是对象名称的前缀。

I've had success with a construct such as:我已经成功地构建了一个结构,例如:

Get-S3Object -BucketName $bucket -KeyPrefix $key |
    Copy-S3Object -LocalFolder $localPath

You can use the AWSCLI commands for S3 in Powershell, they should be already be installed as they come with AWS Powershell Cmdlets.您可以在 Powershell 中使用 S3 的AWSCLI命令,它们应该已经安装,因为它们随 AWS Powershell Cmdlets一起提供。

It's also a much simpler command to understand:这也是一个更容易理解的命令:

aws s3 cp s3://bucketname C:\folder --recursive

检查这些答案,它们类似于Emil的答案,但提供了更多示例: https : //stackoverflow.com/a/53003327/299421https://stackoverflow.com/a/58927950/299421

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

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