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:

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);
    ...
}

      

+3


source to share


1 answer


setRegion

not supported in the version of the AWS SDK you are using. (The docs you linked to are from v2 SDK.)



You can create a new customer and pass in scopes in the designer, for example new Aws\S3\S3Client(['version' => $s3->getApi()->getApiVersion(), 'region' => $loc])

.

+2


source







All Articles