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

时间:2015-05-26 08:36:38

标签: php amazon-web-services amazon-s3

当我尝试使用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 - 类是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,因此我希望能够安装任何依赖项。

1 个答案:

答案 0 :(得分:2)

S3Client::factory()的参数应该是一个数组。你给它一个包含PHP代码的文件名来返回数组,但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);