donglei1973 2015-02-20 09:56
浏览 43

我应该在哪里将AWS凭据集成到我的php框架中?

I am using php mini framework https://github.com/panique/mini

Right now I have the following in the index method in my controller. But it feels like should be better to have it in my config/config.php file. How do I do that so I can use it in my controller/model?

// Instantiate the S3 client with your AWS credentials
        $client = S3Client::factory(array(

        'key' => 'YOUR_AWS_ACCESS_KEY_ID',
        'secret' => 'YOUR_AWS_SECRET_ACCESS_KEY',

        ));

Update

I have now tested two different methods but it is not working. My keys are of course correct.

Using the AWS credentials file and credential profiles

Created a new file in ~/.aws/credentials/credentials.ini with the following code:

[default]
aws_access_key_id = YOUR_AWS_ACCESS_KEY_ID
aws_secret_access_key = YOUR_AWS_SECRET_ACCESS_KEY

But I'm not sure where in the project I should Instantiate a client. I have tested to put the following code in config.php and in my controller but it's not working.

use Aws\S3\S3Client;    
$s3Client = S3Client::factory(array(
        'profile' => 'default',
        'region'  => 'us-west-1',
    ));

Using a configuration file with the service builder

I put this code in 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'
            )
        )
    )
);

And in my controller where I what to access aws I write this in the top of the file.

use Aws\S3\S3Client;
$s3Client = S3Client::factory('APP . '/config/config.php');
$client = $s3Client->get('s3');

But I get the error message

Fatal error: Uncaught exception 'Aws\Common\Exception\InvalidArgumentException' with message 'The config must be provided as an array or Collection.'

  • 写回答

2条回答 默认 最新

  • duancong2965 2015-02-20 19:25
    关注

    Please read the Providing Credentials to the SDK section of the AWS SDK for PHP User Guide. Parts 1, 2, and 3 on that page describe techniques of setting up your credentials without putting them into you application's code or repo.

    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大