适用于 PHP 的 AWS 开发工具包:从实例配置文件元数据服务器检索凭证时出错

我正在尝试通过Web api将SNS消息发送到Android。已从 http://aws.amazon.com/developers/getting-started/php/ 下载并安装 SDK

运行示例时出现以下错误.php:

Fatal error: Uncaught exception 'Aws\Common\Exception\InstanceProfileCredentialsException' with message '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] 28: Connection timed out after 5016 milliseconds [url] http://169.254.169.254/latest/meta-data/iam/security-credentials/)' in C:\xampp\htdocs\aws-php\vendor\aws\aws-sdk-php\src\Aws\Common\InstanceMetadata\InstanceMetadataClient.php:85 Stack trace: #0 C:\xampp\htdocs\aws-php\vendor\aws\aws-sdk-php\src\Aws\Common\Credentials\RefreshableInstanceProfileCredentials.php(52): Aws\Common\InstanceMetadata\InstanceMetadataClient->getInstanceProfileCredentials() #1 C:\xampp\htdocs\aws-php\vendor\aws\aws-sdk-php\src\Aws\Common\Credentials\AbstractRefreshableCredentials.php(54): Aws\Common\Credentials\Refreshable in C:\xampp\htdocs\aws-php\vendor\aws\aws-sdk-php\src\Aws\Common\InstanceMetadata\InstanceMetadataClient.php on line 85

关于这个主题的一点指导将对我帮助很大


答案 1

在我的情况下,我正在使用

return DynamoDbClient::factory(array(
  'version' => 'latest',
  'region'  => AWS_REGION,
  'key' => AWS_KEY,
  'secret'  => AWS_SECRET
));

这曾经与版本2.8.5一起确定,但是当作曲家自动安装版本3.2.0时,我得到了上面的错误。问题很简单,我应该改变我打电话的方式aws/aws-sdk-php

return DynamoDbClient::factory(array(
  'version' => 'latest',
  'region'  => AWS_REGION,
  'credentials' => array(
    'key' => AWS_KEY,
    'secret'  => AWS_SECRET,
  )
));

此处所述。在不更改调用的情况下,apache php 回退到使用 HOME 环境变量查找文件,该变量为空。您可以通过运行 来检查其值。~/.aws/credentialsphp -r 'var_dump(getenv("HOME"));'

这是一篇相关文章


答案 2

在我的情况下,我不得不使用硬编码的凭据

$s3Client = new S3Client([
    'region' => REGION,
    'version' => '2006-03-01',
    'credentials' => [
        'key'    => S3_KEY,
        'secret' => S3_SECRETE,
    ],
]);

在此处查看更多详细信息:


推荐