本文介绍了IAM角色名称在Yii 1中被视为班级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已将该策略附加到EC2 IAM角色以访问AWS服务.之后,我在Yii 1控制器文件中使用了以下代码:

I have attached the policy to EC2 IAM role to access AWS services. After that i have have used below code in Yii 1 controller file:

ExampleController.php

class ExampleController extends Controller
{
    public function init()
    {
        require_once dirname(dirname(__FILE__)) . '/extensions/awsv3/vendor/autoload.php';
        $config = array(
                'version' => 'latest',
                'region' => 'us-west-2',
        );
        $s3_instance = new \Aws\Ssm\SsmClient($config);
        $result = $s3_instance->getParameters([
            'Names' => array('host_name'),
            'WithDecryption' => true
        ]);
        //converting S3 private data to array to read
        $keys = $result->toArray();
        var_dump($keys);
        exit("Exit");
    }
}

输出

注意:TestRole是IAM角色名称.

Note: TestRole is IAM Role Name.

我在单个PHP文件中使用了相同的代码(与Yii1无关)

I have used same code in Single PHP file (Not ties with Yii1)

test.php

require_once 'protected/extensions/awsv3/vendor/autoload.php';
$config = array(
        'version' => 'latest',
        'region' => 'us-west-2',
);
$s3_instance = new \Aws\Ssm\SsmClient($config);
$result = $s3_instance->getParameters([
    'Names' => array('host_name'),
    'WithDecryption' => true
]);
//converting S3 private data to array to read
$keys = $result->toArray();
var_dump($keys);
exit("Exit");

使用单个php文件.

所以问题是如何在Yii 1中修​​复它,以及为什么要将IAM角色名称视为Class文件?

So question is how to fix it in Yii 1 and why its considering IAM Role Name as Class file?

推荐答案

由于@javierfdezg,我得以解决此问题.

I was able to fix this out, Thanks to @javierfdezg.

因此,基本上,Yii的自动加载器和AWS的自动加载器发生冲突,并且可能是由于Yii认为类名必须与文件名匹配所致.

So basically, Yii's auto loader and AWS's auto loader was got conflicted and may be due to Yii's assumption that class names must match file names.

因此,首先我已取消注册Yii的自动加载程序,然后在api调用完成后再次对其进行注册.

So first i have unregistered the Yii's auto load then after the api call is finished registered it again.

class ExampleController extends Controller
{
    public function init()
    {
        /* Unregister  YiiBase */
        spl_autoload_unregister(array('YiiBase', 'autoload'));
        require_once dirname(dirname(__FILE__)) . '/extensions/awsv3/vendor/autoload.php';
        $config = array(
                'version' => 'latest',
                'region' => 'us-west-2',
        );
        $s3_instance = new \Aws\Ssm\SsmClient($config);
        $result = $s3_instance->getParameters([
            'Names' => array('host_name'),
            'WithDecryption' => true
        ]);

        /* Register  YiiBase */
        spl_autoload_register(array('YiiBase', 'autoload'));


        $keys = $result->toArray();
        var_dump($keys);
        exit("Exit");
    }
}

这篇关于IAM角色名称在Yii 1中被视为班级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 01:40