当我在服务构建器中使用AWS配置文件时,我会得到Undefined变量


I get Undefined variable when I am using AWS configuration file with the service builder

我正在尝试集成这个PHP框架https://github.com/panique/mini使用AmazonS3的配置文件和服务构建器http://docs.aws.amazon.com/aws-sdk-php/guide/latest/credentials.html#using-a-configuration-file-with-the-service-builder

最好是我能将它与现有的config.php集成https://github.com/panique/mini/blob/master/application/config/config.php但我不知道该怎么处理这条线。

$aws = Aws::factory('/path/to/custom/config.php');

由于我已经在代码的另一部分中包含了config.php

这是我尝试过的,但不知道为什么它不起作用

在config文件夹中创建了一个新文件aws-config.php,并将其包含在我的项目中。aws-config.php有以下代码(使用我正确的密钥)。

return array(
    // Bootstrap the configuration file with AWS specific features
    'includes' => array('_aws'),
    'services' => array(
        // All AWS clients extend from 'default_settings'. Here we are
        // overriding 'default_settings' with our default credentials and
        // providing a default region setting.
        'default_settings' => array(
            'params' => array(
                array(
                    'credentials' => array(
                        'key'    => 'YOUR_AWS_ACCESS_KEY_ID',
                        'secret' => 'YOUR_AWS_SECRET_ACCESS_KEY',
                    )
                ),
                'region' => 'us-west-1'
            )
        )
    )
);

我想访问我的控制器中的凭据,如下所示:https://github.com/panique/mini/blob/master/application/controller/songs.php

我已经从文档中实现了它

<?php
use Aws'S3'S3Client;
use Aws'Common'Aws;
// Create the AWS service builder, providing the path to the config file
$aws = Aws::factory(APP . 'config/aws-config.php');
$client = $aws->get('s3');
class Album extends Controller
{
    public function index()
    {
            foreach ($images as &$image) {
                $image->imageThumbnailUrl = $client->getObjectUrl($resizedBucket, 'resized-'.$image->image_name, '+10 minutes');
            }
...
...

我收到错误信息

注意:未定义的变量:客户端出现致命错误:调用成员函数getObjectUrl()在中的非对象上

我在循环中使用$client和getObjectUrl。

如果我使用"将凭据传递到客户端工厂方法",我的代码运行良好http://docs.aws.amazon.com/aws-sdk-php/guide/latest/credentials.html#passing-credentials-int-a-client-factory方法在我的控制器中的index方法中。

这里的问题与AWS SDK或panique/mini框架无关。不能在类定义之外声明变量,并期望能够在类定义内部使用它们。这就是PHP中变量作用域的工作方式。您需要以某种方式将s3Client对象传递到控制器中,或者在控制器内部实例化它。

您可以将这些行移动到索引方法中,它应该可以工作。

// Create the AWS service builder, providing the path to the config file
$aws = Aws::factory(APP . 'config/aws-config.php');
$client = $aws->get('s3');