尝试实例化 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
我正在通过 composer
"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 profiles
和 environment 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
最佳答案
您应该将 .aws/credentials
文件放在 Web 服务的根目录中,而不是放在 ssh 用户的根目录中
关于php - 无法实例化\Aws\Ec2\Ec2Client,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27705783/