简体   繁体   中英

Where should I integrate AWS credentials in my php framework?

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.'

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.

Have you tried requiring the composer autoloader

require 'vendor/autoload.php'

Then you can instantiate your client anywhere in your project

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM