2

I copy the content of an S3 bucket to a local directory, however I get an error output from the powershell.

Copy-S3Object : The requested range is not satisfiable

It is pointing to this command:

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

Why do I get this error ? Note that the desired files that are needed to be copied do indeed get copied locally.

Anthony Neace
  • 25,013
  • 7
  • 114
  • 129
Sam Gomari
  • 733
  • 4
  • 13
  • 37

3 Answers3

3

I can't say why you are getting that error returned from S3, but I can tell you that if you are copying multiple objects you probably want to use the -LocalFolder parameter, not -LocalFile. -LocalFolder will preserve the prefixes as subpaths.

When downloading one or more objects from S3, the Read-S3Object cmdlet works the same as Copy-S3Object, but uses -KeyPrefix to specify the common prefix the objects share, and -Folder to indicate the folder they should be downloaded to.

This also reminds me I need to check why we used -LocalFolder on Copy-, and -Folder on Read- although I suspect aliases may also be available to make them consistent.

HTH

(Edit): I spent some time this morning reviewing the cmdlet code and it doesn't appear to me the cmdlet would work as-is on a multi-object download, even though it has a -LocalFolder parameter. If you have a single object to download, then using -Key/-LocalFile is the correct parameter combination. If -LocalFolder is passed, the cmdlet sets up internally to do a single file download instead of treating -Key as a common key prefix to a set of objects. So, I think we have a bug here that I'm looking into.

In the meantime, I would use Read-S3Object to do your downloads. It supports both single (-Key) or multi-object download (-KeyPrefix) modes. https://docs.aws.amazon.com/powershell/latest/reference/index.html?page=Read-S3Object.html&tocid=Read-S3Object

Steve Roberts
  • 714
  • 4
  • 8
  • I have been getting System.InvalidOperationException: The requested range is not satisfiable from Read-S3Object -BucketName $BucketName -KeyPrefix $BuildVersion -Folder $DeploymentDir for the last couple of days for no reason I can discern. I don't suppose that there are any other issues lurking that we may need to be aware of? – Brendon Bezuidenhout Jul 26 '17 at 14:17
3

this seems to occur with folders that do not contain files since copy wants to copy files. i accepted this error and trapped it.

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

  }
Kelly Davis
  • 354
  • 2
  • 6
  • I've also had it happen, fwiw, when I do get-s3object on a "folder", then forget to exclude the folder when doing the read-s3object. – mbourgon Oct 29 '19 at 06:20
0

This happened to me when I tried to download the file which had more than one dots in it. Simplifying the file name, fixed the error.

File name that gave me error: myfile-18.10.exe
File name that worked: myfile-1810.exe
Asdfg
  • 11,362
  • 24
  • 98
  • 175