-1

I am searching on the internet on how can I get the AWS s3 bucket region with an API call or directly in PHP using their library but have not luck finding the info.

I have the following info available: Account credentials, bucket name, access key + secret. That is for multiple buckets, that I have access to, and I need to get the region programatically, so logging in to aws console and checking out is not an option.

naneri
  • 3,771
  • 2
  • 29
  • 53

2 Answers2

1

Assuming you have an instance of the AWS PHP Client in $client, you should be able to find the location with $client->getBucketLocation().

Here is some example code:

<?php

$result = $client->getBucketLocation([
    'Bucket' => 'yourBucket',
]);

The result will look like this

[
    'LocationConstraint' => 'the-region-of-your-bucket',
]
pmatsumura
  • 391
  • 1
  • 6
  • How can I instantiate AWS client? It requires the region it seems :/ – naneri Mar 05 '19 at 05:15
  • You should be able to pick any of the regions, since the call to `getBucketLocation()` is bucket agnostic. You can find a list of available regions at [AWS Regions and Endpoints](https://docs.aws.amazon.com/general/latest/gr/rande.html#s3_region). – pmatsumura Mar 05 '19 at 08:19
1

When you create a S3 client, you can use any of the available regions in AWS, even if it's not one that you use.

$s3Client = new Aws\S3\S3MultiRegionClient([
            'version'     => 'latest',
            'region'      => 'us-east-1',
            'credentials' => [
                'key'    => $accessKey,
                'secret' => $secretKey,
            ],
        ]);

$region = $s3Client->determineBucketRegion($bucketname);
Machavity
  • 30,841
  • 27
  • 92
  • 100
Ravi Jayagopal
  • 916
  • 7
  • 10