由于复制 S3 存储桶的内容导致 PowerShell 出错

Error in PowerShell due to copying the content of an S3 bucket

我将 S3 存储桶的内容复制到本地目录,但是我从 powershell 中收到错误输出。

Copy-S3Object : The requested range is not satisfiable

它指向这个命令:

Copy-S3Object -BucketName $bucket -Key $object.Key -LocalFile $localFilePath -Region $region

为什么会出现此错误?请注意,需要复制的所需文件确实会在本地复制。

我不能说为什么你会收到从 S3 返回的错误,但我可以告诉你,如果你正在复制多个对象,你可能想要使用 -LocalFolder 参数,而不是 -LocalFile。 -LocalFolder 会将前缀保留为子路径。

从 S3 下载一个或多个对象时,Read-S3Object cmdlet 与 Copy-S3Object 的工作方式相同,但使用 -KeyPrefix 指定对象共享的公共前缀,并使用 -Folder 指示它们应该位于的文件夹下载到.

这也提醒我,我需要检查为什么我们在复制时使用 -LocalFolder,而在读取时使用 -Folder- 尽管我怀疑别名也可以使它们保持一致。

HTH

(编辑):今天早上我花了一些时间查看 cmdlet 代码,但在我看来,该 cmdlet 在多对象下载时不会按原样工作,即使它有一个 -LocalFolder 参数。如果要下载单个对象,则使用 -Key/-LocalFile 是正确的参数组合。如果传递 -LocalFolder,cmdlet 将在内部设置为执行单个文件下载,而不是将 -Key 视为一组对象的公共键前缀。所以,我认为我们这里有一个错误,我正在调查。

与此同时,我会使用 Read-S3Object 进行下载。它支持单(-Key)或多对象下载(-KeyPrefix)模式。 https://docs.aws.amazon.com/powershell/latest/reference/index.html?page=Read-S3Object.html&tocid=Read-S3Object

这似乎发生在不包含文件的文件夹中,因为复制要复制文件。 我接受了这个错误并困住了它。

catch  [Amazon.S3.AmazonS3Exception]
{

  # get error record
  [Management.Automation.ErrorRecord]$e = $_

  # retrieve information about runtime error
  $info = [PSCustomObject]@{
    Exception = $e.Exception.Message
    Reason    = $e.CategoryInfo.Reason
    Target    = $e.CategoryInfo.TargetName
    Script    = $e.InvocationInfo.ScriptName
    Line      = $e.InvocationInfo.ScriptLineNumber
    Column    = $e.InvocationInfo.OffsetInLine
    ErrorCode = $e.Exception.ErrorCode
  }
  if ($info.ErrorCode="InvalidRange") { #do nothing 
  } Else {
  # output information. Post-process collected info, and log info (optional)
  write-host $info -ForegroundColor Red}
}

  }

当我试图下载其中有多个点的文件时,我遇到了这种情况。简化文件名,修复错误

File name that gave me error: myfile-18.10.exe
File name that worked: myfile-1810.exe