Call to S3Client :: setRegion () fails
I am using "aws/aws-sdk-php": "3.0.3"
via composer to access some S3 buckets in different regions, but I cannot get S3Client to modify regions using a function setRegion()
without generating an error:
PHP Catchable fatal error: argument 2 passed to Aws \ AwsClient :: getCommand () must be from an array of type given on the line called in vendor / aws / aws-sdk-php / src / AwsClient.php on line 166 and defined in vendor / aws / aws-sdk-php / src / AwsClient.php on line 210
code:
foreach($buckets as $bucket) {
echo 'foo' . PHP_EOL;
$loc = $s3->getBucketLocation(['Bucket' => $bucket])['LocationConstraint'];
var_dump($loc);
$s3->setRegion($loc);
echo 'bar' . PHP_EOL;
$years = $s3->listObjects([
'Bucket' => $bucket,
'Delimiter' => '/'
]);
var_dump($bucket, $years);
}
Output:
foo
string(9) "us-east-1"
PHP Catchable fatal error: {snip}
Notes:
- S3Client Docs say the method inherits from
Aws\Common\Client\AbstractClient
-
Aws\Common\Client\AbstractClient
The docs say the function takes a string - But it looks like it comes down to it for some reason
Aws\AwsClient::__call()
?
change
As @giaour said, S3Client::setRegion()
no longer exists in the v3 client and the documentation I linked was for v2. [I have no idea why it is labeled "last")
Here is the code I implemented as a workaround, which now only aligns to "canonical":
protected function s3($region='us-west-2') {
if( ! isset($this->_clients[$region]) ) {
$this->_clients[$region] = new Aws\S3\S3Client([
'version' => $this->_aws_version,
'region' => $region,
'credentials' => $this->credentials
]);
}
return $this->_clients[$region];
}
And then:
foreach($buckets as $bucket) {
$region = $this->s3()->getBucketLocation(['Bucket' => $bucket])['LocationConstraint'];
$s3 = $this->s3($region);
...
}
source to share