【问题标题】:Unable to instantiate \Aws\Ec2\Ec2Client无法实例化 \Aws\Ec2\Ec2Client
【发布时间】:2014-12-30 12:46:01
【问题描述】:

尝试实例化 ec2client w.r.t

$ec2Client = \Aws\Ec2\Ec2Client::factory(array(
    'profile' => 'default',
    'region'  => 'us-west-2',
    'version' => '2014-10-01'
));

但因错误而失败

Uncaught exception 'Aws\Common\Exception\CredentialsException' with message 'Could not load credentials.'

我有:

cat ~/.aws/credentials 

[default]
aws_access_key_id = xxxxxxxxxxx
aws_secret_access_key = xxxxxxxxxxx

我通过作曲家"aws/aws-sdk-php": "3.*@dev" 使用aws-sdk。

编辑 1):我正在本地开发机器上尝试这个。

编辑 2): 我试过这个:

 use Aws\Common\Credentials\InstanceProfileProvider;
    $profile= new InstanceProfileProvider(['profile'=>'default']);
    $ec2Client = \Aws\Ec2\Ec2Client::factory(array(
        'region'  => 'us-west-2',
        'version' =>'2014-10-01',
        'credentials' => $profile
    ));

这给了我不同的错误:

'Error retrieving credentials from the instance profile metadata server. When you are not running inside of Amazon EC2, you must provide your AWS Access Key ID and Secret Access Key in the "key" and "secret" options when creating a client or provide an instantiated Aws\Common\Credentials\CredentialsInterface object. (cURL error 28: Connection timed out after 1000 milliseconds)' in .. /InstanceProfileProvider.php:9 ..

这给我的印象是凭据配置文件仅在应用程序从 AWS 云实例中运行时才有效!这使得配置文件的想法毫无用处(用于开发环境)

编辑 3)

调试后,带有凭据配置文件的 sdk 似乎已损坏或未达到预期。 credential profilesenvironment variable 都依赖于环境变量。如果环境变量被禁用,两者都会失败。

可能的解决方法:

a)Enable environment variables

b) 设置 HOME evn 变量

putenv('HOME=/Users/yourname');
$ec2Client = \Aws\Ec2\Ec2Client::factory(array(
'region'  => 'us-west-2',
'version' => '2014-10-01'
));

Profile init() 函数具有文件名选项,但除了HOME env 变量之外,没有明显的方法可以传递凭据配置文件路径

Aws\Common\Credentails\Profiler->init()
public static function ini($profile = null, $filename = null){
...
}

同样,如果您无法读取 /Users/yourname/.aws/credentials 文件,您必须稍微调整一下

chmod 644 /Users/yourname/.aws/credentials

【问题讨论】:

标签: php aws-sdk


【解决方案1】:

您应该将.aws/credentials 文件放在您的网络服务的根目录中,而不是放在您的 ssh 用户的根目录中

【讨论】:

    【解决方案2】:

    您必须将 .aws/credentials 文件与您的配置一起放在 Web 服务的主目录中。

    把它放到你网站的某个 PHP 文件中

    echo getenv('HOME');
    

    然后在那个目录中:

    mkdir .aws
    touch credentials
    nano credentials
    

    你必须在那里粘贴内容(键中不需要引号):

    [default]
    aws_access_key_id     = xxxxxxxxxxx
    aws_secret_access_key = xxxxxxxxxxx
    

    应该这样做!


    编辑: 正如 sakhunzai 在评论中所说(谢谢!),echo getenv('HOME') 有时会返回 null,如果发生这种情况,他提供了这个解决方案:“但是从命令行 php -i |grep HOME 给了我正确的用户主目录”

    请记住这一点。

    【讨论】:

    • echo getenv('HOME'); return null,除非我设置它putenv('HOME=/Users/yourname'); 但是从命令行 php -i |grep HOME 给了我正确的用户主目录
    猜你喜欢
    • 2020-08-19
    • 2020-12-23
    • 1970-01-01
    • 1970-01-01
    • 2015-11-19
    • 2021-08-25
    • 2018-01-03
    • 2016-08-24
    • 1970-01-01
    相关资源
    最近更新 更多