<?php
require 'vendor/autoload.php';

use Aws\S3\S3Client;
use Aws\Exception\AwsException;

// Replace with your AWS access key and secret key
$accessKey = 'AKIAXRET5SHQB7HVOIKN';
$secretKey = 'G9rT6ZVm/czOaKTOUFEKxRaMZQx0kpY7SYGRED7W';
$bucketName = 'mytime-sparkle';

// Create an S3 client
$s3 = new S3Client([
    'version' => 'latest',
    'region' => 'us-east-1', // You can use any region here; it won't matter for the request
    'credentials' => [
        'key'    => $accessKey,
        'secret' => $secretKey,
    ],
]);

try {
    // Use the getBucketLocation method to get the bucket's region
    $result = $s3->getBucketLocation([
        'Bucket' => $bucketName,
    ]);

    // The region is returned in the 'LocationConstraint' field
    $region = $result['LocationConstraint'] ?: 'us-east-1'; // Default region if no location constraint
    echo "The region for bucket '$bucketName' is: $region\n";
} catch (AwsException $e) {
    // Output error message if fails
    echo "Error: " . $e->getMessage() . "\n";
}
?>
