如何从s3服务下载文件到本地文件夹

How to download files from s3 service to local folder

我需要从简单存储服务下载文件到本地文件夹 并计算本地文件夹中的 no.of 个文件并检查简单存储服务,然后发送包含文件数量的邮件。

我尝试从简单的存储服务下载文件,但出现 get-s3object commandnotfoundexception 之类的错误。我该如何解决这个问题?

Code reference I have taken

# Your account access key - must have read access to your S3 Bucket
$accessKey = "YOUR-ACCESS-KEY"
# Your account secret access key
$secretKey = "YOUR-SECRET-KEY"
# 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 = "my-test-bucket"
# The folder in your bucket to copy, including trailing slash. Leave blank to copy the entire bucket
$keyPrefix = "my-folder/"
# The local file path where files should be copied
$localPath = "C:\s3-downloads\"    

$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
    }
}

如果您安装了 AWS PowerShell 模块,则说明您尚未将其正确加载到当前会话中。我们将此确定为问题,因为您指定的错误意味着找不到给定的 cmdlet。

首先通过以下任一选项验证模块是否已安装:

将模块加载到现有会话中:(PowerShell v3 和 v4):

来自文档:

In PowerShell 4.0 and later releases, Import-Module also searches the Program Files folder for installed modules, so it is not necessary to provide the full path to the module. You can run the following command to import the AWSPowerShell module. In PowerShell 3.0 and later, running a cmdlet in the module also automatically imports a module into your session.

要验证安装是否正确,请将以下命令添加到脚本的开头:

PS C:\> Import-Module AWSPowerShell

将模块加载到现有会话中:(PowerShell v2):

要验证安装是否正确,请将以下命令添加到脚本的开头:

PS C:\> Import-Module "C:\Program Files (x86)\AWS Tools\PowerShell\AWSPowerShell\AWSPowerShell.psd1"

使用 Windows PowerShell for AWS Desktop Shortcut 打开一个新会话:

桌面上添加了一个快捷方式,可以启动 PowerShell 并将正确的模块加载到会话中。如果您的安装成功,则该快捷方式应该存在并且还应该正确加载 AWS PowerShell 模块,您无需额外操作。

来自文档:

The installer creates a Start Menu group called, Amazon Web Services, which contains a shortcut called Windows PowerShell for AWS. For PowerShell 2.0, this shortcut automatically imports the AWSPowerShell module and then runs the Initialize-AWSDefaults cmdlet. For PowerShell 3.0, the AWSPowerShell module is loaded automatically whenever you run an AWS cmdlet. So, for PowerShell 3.0, the shortcut created by the installer only runs the Initialize-AWSDefaults cmdlet. For more information about Initialize-AWSDefaults, see Using AWS Credentials.

进一步阅读:

因为这个问题是 "powershell download s3 files" 的前 Google 结果之一,我将回答标题中的问题(即使实际问题文本不同):

Read-S3Object -BucketName "my-s3-bucket" -KeyPrefix "path/to/directory" -Folder .

如果不是 public 存储桶,您可能需要调用 Set-AWSCredentials

与 Will 的示例类似,如果您想下载一个 "folder" 的全部内容并保持目录结构,请尝试:

Get-S3Object -BucketName "my-bucket" -KeyPrefix "path/to/directory" | Read-S3Object -Folder .

https://docs.aws.amazon.com/powershell/latest/reference/items/Read-S3Object.html 的 MS 文档提供了更高级过滤的示例。