aws / aws-tools-for-powershell

The AWS Tools for PowerShell lets developers and administrators manage their AWS services from the PowerShell scripting environment.
Apache License 2.0
238 stars 79 forks source link

Get-S3BucketLocation returns invalid, shorter Region Codes #37

Closed juancrl closed 2 years ago

juancrl commented 5 years ago

In some cases, Get-S3BucketLocation -bucketname XXX returns a shorter Region Code than expected (Amazon.S3.S3Region).

In my case, I have several buckets in eu-west-1 (Ireland), Calling Get-S3BucketLocation on them returns a value of "EU", and not "EU-WEST-1".

This is a problem when using the returned region code in the next cmdlets (for example, Set-S3Object -Region...), "EU" is not accepted as a valid Region, ir expects "EU-WEST-1". The operation takes a long time and finally times out. With the valid region all is fine.

Expected behaviour 1 : Get-S3BucketLocation should always return a region code which can be used by all cmdlets 2 : All cmdlets using a Region should validate it before any traffic is attempted. 3 : Get-S3Bucket should contain the region in the object (as requested in other issue), being a property on the bucket object itself; no need to call explicitly Get-BucketLocation.

Example which fails :

$Filename1 = $Env:Temp + "\" + "testfile11.txt";  "Test file 1" > $Filename1
$AllBuckets = Get-S3Bucket
$BucketChosen = $AllBuckets  |  Get-Random -Count 1     # If eu-west-1
$BucketName   = $BucketChosen.BucketName
$Location = (Get-S3BucketLocation -BucketName $BucketName).Value              
             # can return "EU"   "locality"  
   Write-S3Object  -BucketName  $BucketName -Region $Location -File $FileName1  -CannedACLName public-read        

   ## Using wrong region "EU" takes several minutes to timeout, and finally
   ## Write-S3Object : An error occurred while sending the request.

   if substituting "EU" with "EU-WEST-1" then all is fine
matteo-prosperi commented 5 years ago

Hello, Thank you for providing this feedback.

  1. Get-S3BucketLocation behaves according to https://docs.aws.amazon.com/AmazonS3/latest/API/RESTBucketGETlocation.html. It returns a Location Constraint object that can be converted to a region using the table at https://docs.aws.amazon.com/general/latest/gr/rande.html#s3_region. I will consider this for future improvements, but it may be difficult to do so without breaking backward compatibility.
  2. Not validating the region before attempting the network request is done in order to allow forward compatibility of the module. You can use an older module with a newly launched AWS region and the AWS SDK for .NET (which the AWS Tools for PowerShell leverage) will try its best to identify the endpoint for that unknown region.
  3. Answered in of https://github.com/aws/aws-tools-for-powershell/issues/36.

I will keep this issue open and see what we can do to fix the region value returned by Get-S3BucketLocation.

juancrl commented 3 years ago

I am discovering new side effects, showing the region incoherence in multiple places. If you create a bucket from the console in eu-west-1, Get-S3BucketLocation will then show the right "LocationConstraint" (eu-west-1). But if you create it with New-S3Bucket -region "eu-west-1", Get-S3BucketLocation will just show "EU". This difference filters the bucket in other tools ! For example, when you run Glue in eu-west-1 region, it won't show you the buckets created in PowerShell ("The bucket xxxx was found in EU instead of eu-west-1. Select a bucket that was created in eu-west-1") There's a S3 client in https://www.powershellgallery.com/packages/S3-Client/1.19.0 that seems to accept or use the right location. Maybe it could be used...

juancrl commented 3 years ago

From S3-Client code :

    # AWS does not allow to set LocationConstraint for default region us-east-1
    if ($Config.Region -ne "us-east-1") {
        $RequestPayload = "<CreateBucketConfiguration xmlns=`"http://s3.amazonaws.com/doc/2006-03-01/`"><LocationConstraint>$($Config.Region)</LocationConstraint></CreateBucketConfiguration>"
    }

Works like a charm... (This is important as many European users will default to eu-west-1)

github-actions[bot] commented 2 years ago

We have noticed this issue has not received attention in 1 year. We will close this issue for now. If you think this is in error, please feel free to comment and reopen the issue.