S3Client PHP SDK:class <class> 的对象无法转换为字符串

S3Client PHP SDK: Object of class <class> could not be converted to string

当我尝试使用 AWS PHP SDK 时出现以下错误:

PHP Warning:  Illegal string offset 'client.backoff' in C:\xampp\htdocs\aws_test_local\vendor\aws\aws-sdk-php\src\Aws\S3\S3Client.php on line 172

PHP Catchable fatal error:  Object of class Guzzle\Plugin\Backoff\BackoffPlugin could not be converted to string in C:\xampp\htdocs\aws_test_local\vendor\aws\aws-sdk-php\src\Aws\S3\S3Client.php on line 172

PHP Warning:  Illegal string offset 'signature' in C:\xampp\htdocs\aws_test_local\vendor\aws\aws-sdk-php\src\Aws\S3\S3Client.php on line 175

PHP Catchable fatal error:  Object of class Aws\S3\S3Signature could not be converted to string in C:\xampp\htdocs\aws_test_local\vendor\aws\aws-sdk-php\src\Aws\S3\S3Client.php on line 175

它们源自 AWS SDK S3Client.php 文件部分中的以下代码。

public static function factory($config = array())
{
    $exceptionParser = new S3ExceptionParser();

    // Configure the custom exponential backoff plugin for retrying S3 specific errors
    if (!isset($config[Options::BACKOFF])) {
        $config[Options::BACKOFF] = static::createBackoffPlugin($exceptionParser);
    }

    $config[Options::SIGNATURE] = $signature = static::createSignature($config);
...

Options-class就是Aws\Common\Enum\ClientOptions。如果你看它,它定义了很多这样的常量:

const SIGNATURE = 'signature';
const BACKOFF = 'client.backoff';

我是这样调用工厂函数的:

$s3 = S3Client::factory(_PS_ROOT_DIR_.'/override/aws/aws-config.php');

我的 aws-config.php 文件如下所示:

<?php

    return array(
        'includes' => array('_aws'),
        'services' => array(
            'default_settings' => array(
                'params' => array(
                    'key'    => 'XXXXXXXXXXX',
                    'secret' => 'XXXXXXXXXXX',
                    'region' => 'eu-west-1'
                )
            )
        )
    );

?>

有什么想法吗?我安装了带有 Composer 的 PHP SDK,因此我希望安装任何依赖项。

S3Client::factory() 的参数应该是一个数组。您给它的文件名包含 PHP 代码到 return 数组,但 S3Client 没有 运行 文件。尝试将文件更改为:

<?php

    $s3options = array(
        'includes' => array('_aws'),
        'services' => array(
            'default_settings' => array(
                'params' => array(
                    'key'    => 'XXXXXXXXXXX',
                    'secret' => 'XXXXXXXXXXX',
                    'region' => 'eu-west-1'
                )
            )
        )
    );

?>

那么你的主程序可以做:

require(_PS_ROOT_DIR_.'/override/aws/aws-config.php');
$s3 = S3Client::factory($s3options);