php - 无法实例化\Aws\Ec2\Ec2Client

标签 php aws-sdk

尝试实例化 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 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 变量外,没有明显的方式传递 credentails 配置文件路径

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 文件放在您的网络服务的根目录中,而不是您的 ssh 用户的根目录中

关于php - 无法实例化\Aws\Ec2\Ec2Client,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27705783/

相关文章:

php - 计算网站拥有的数量或发布参数

php - 配方/成分/测量/数量的数据库模式

java - 列出AWS S3存储桶的内容

java - 用 Java 创建 S3 客户端时出错

php - 使用 array_fill 创建对象数组时如何使每个对象唯一?

javascript - AJAX 请求中所请求的资源上不存在 'Access-Control-Allow-Origin' header

javascript - AWS SDK S3-如何在 putObject 方法中包含凭据

java - 使用资源 URL 删除 AWS S3 资源 - Java SDK

amazon-web-services - 通过 AWS SDK for Go 上传到 Device Farm 的 Android 应用程序从未更改过 INITIALIZED 状态

php - 从数据库查询中排除一些值